这是一个聊天回复程序,我目前面临的问题是,如果输入的答案与我的一个答案不匹配,我会尝试让它退出,但由于这三个问题的所有答案都是作为响应放在一起的,所以即使是它随机选择的问题的正确答案也会被输入,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()
Python使用elif为此提供了良好的流控制结构。请参阅此处的官方文档:https://docs.python.org/3/tutorial/controlflow.html
所以我们的想法是所有的语句都是作为一个单元来计算的。
现在语句将一直运行,直到一个是
True
。如果都是false
,它将默认为else
语句。而不是
执行以下操作:
否则,如果第1个
if
为false,您将退出而不检查其他的。相关问题 更多 >
编程相关推荐