上面你提供的代码少了User类
时间: 2023-12-14 10:36:10 浏览: 25
非常抱歉,我漏掉了User类的代码。以下是完整的代码:
```python
import random
class User:
def __init__(self, name):
self.name = name
self.history = []
def say(self, text):
self.history.append(text)
print(f"{self.name}: {text}")
def introduce(self):
self.say(f"Hi, I'm {self.name}!")
def chat(self):
pass
class Bot:
def __init__(self, name):
self.name = name
self.history = []
def say(self, text):
self.history.append(text)
print(f"{self.name}: {text}")
def introduce(self):
self.say(f"Hello, I'm {self.name}! How can I help you today?")
def chat(self):
pass
class SimpleBot(Bot):
def __init__(self, name):
super().__init__(name)
self.greetings = ["hi", "hello", "hey", "hola", "bonjour"]
self.goodbyes = ["bye", "goodbye", "see you", "adios", "au revoir"]
self.questions = ["how are you", "what's up", "how's it going", "how's life"]
self.answers = ["I'm fine, thanks.", "Nothing much.", "It's going well.", "Life is good."]
def chat(self):
user_input = input("> ").lower()
if user_input in self.greetings:
self.say(random.choice(self.greetings).capitalize())
elif user_input in self.goodbyes:
self.say(random.choice(self.goodbyes).capitalize())
elif user_input in self.questions:
self.say(random.choice(self.answers))
else:
self.say("I'm sorry, I didn't understand what you said.")
user = User("Alice")
bot = SimpleBot("Bot")
bot.introduce()
user.introduce()
while True:
user.chat()
bot.chat()
```
这个版本的代码包含了用户和机器人的类定义,以及一个简单的机器人实现。