2、修改上一题程序,要求使用带参数的构造函数赋初值;
时间: 2024-02-06 19:10:34 浏览: 60
C++类总结多态继承
4星 · 用户满意度95%
以下是使用带参数构造函数赋初值的代码:
```python
import random
class ChitChat:
def __init__(self, name):
self.name = name
def greeting(self):
greetings = [
"Hi, I'm {0}. How can I help you today?".format(self.name),
"Hello there! My name is {0}. What can I do for you?".format(self.name),
"Hey! This is {0}. How may I assist you?".format(self.name)
]
return random.choice(greetings)
def farewell(self):
farewells = [
"It was nice chatting with you. Goodbye!",
"Goodbye! Have a great day!",
"See you later. Take care!"
]
return random.choice(farewells)
chatbot = ChitChat("ChitGPT")
print(chatbot.greeting())
user_input = input()
while user_input.lower() != 'bye':
response = "I'm sorry, I don't understand. Could you please rephrase that?"
print(response)
user_input = input()
print(chatbot.farewell())
```
阅读全文