这是一个聊天响应程序,我目前面临的问题是,如果输入的答案与我的答案不匹配,我会尝试退出,但由于这三个问题的所有答案都放在了一起作为响应,所以甚至输入随机选择的问题的权利,python仍然认为它是不对的,因为它不符合其他两个,然后它将退出。请告诉我如何对这个程序采取不同的方法。谢谢你帮助1
import random
x=input("What is your name? ")
def main():
def feeling():
return input("How are you feeling right now " +str(x)+"? ")
def homesick():
return input("Do you miss your home? ")
def miss():
return input("Who do you miss?")
prompts = [feeling,homesick,miss]
response = random.choice(prompts)()
if response==("tired"):
Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print(random.choice(Tired))
else:
exit()
if response==("yes"):
yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print(random.choice(yes))
else:
exit()
if response==("my mom"):
print("Mom will be in town soon")
else:
exit()
main()
main()
答案 0 :(得分:2)
Python使用elif为此提供了很好的流控制结构。请参阅此处的官方文档:https://docs.python.org/3/tutorial/controlflow.html
因此,我们的想法是将所有陈述作为一个单元进行评估。
if response==("tired"):
Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print(random.choice(Tired))
elif response==("yes"):
yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print(random.choice(yes))
elif response==("my mom"):
print("Mom will be in town soon")
else:
exit()
现在语句将一直运行到True
。如果全部为false
,则默认为else
语句。
答案 1 :(得分:0)
而不是
if
else: exit
if
else: exit
if
else: exit
这样做:
if
elif:
elif:
else: exit
答案 2 :(得分:0)
您应该通过聊天机器人对提示和预期答案以及回复进行分组,例如:作为(prompt, expected_answer, [some, possible, replies])
feeling = ("How are you feeling right now " +str(x)+"? ", "tired",
['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.'])
miss = ...
homesick = ...
prompts = [feeling,homesick,miss]
choice = random.choice(prompts)()
prompt, ans, replies
resp = input(promp)
if resp == ans:
print(random.choice(replies))
else:
exit()
这样还可以更方便地使用更多问题/答案对扩展聊天机器人,或者为不同的用户答案提供不同的聊天机器人回复。
答案 3 :(得分:0)
import random
name = input("What is your name? ")
def main():
def feeling():
response = input("How are you feeling right now {name}?".format(name=name))
if response == "tired":
tired = ['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print(random.choice(tired))
else:
exit()
def homesick():
response = input("Do you miss your home? ")
if response == "yes":
yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print(random.choice(yes))
else:
exit()
def miss():
response = input("Who do you miss?")
if response == "my mom":
print("Mom will be in town soon")
else:
exit()
prompts = [feeling, homesick, miss]
random.choice(prompts)()
main()
答案 4 :(得分:0)
不了解您的代码,但您可以执行以下操作:
if response!=("tired"):
exit()
return
Tired=['I wish I can make you feel better.','I hope school is not making you feel stressed.','You deserve the right to relax.']
print(random.choice(Tired))
if response!=("yes"):
exit()
return
yes=["Don't worry, you will be home soon......",'I am protecting your family and loved ones, trust me on this.',"Your kingdoms has been waiting for a long time, they'd forgiven your mistakes"]
print(random.choice(yes))
[...]