如何使聊天机器人识别小写和大写的答案

时间:2018-01-11 12:20:46

标签: python

我正在努力让我的python聊天机器人认识到我用大写和小写给出的答案。 这是我的代码中的一个问题。 (所有代码都以您在下面看到的格式编写)

 Q1 = input ("If you could have any pet what would it be?")
 if Q1 == "hamster":
     print ("I would love a pet hamster")
 elif Q1 in "dog,cat,rabbit,gerbil,fish,rat":
     print ("Not my first choice, but still good!")
 else:
     print ("Not bad.")

提前感谢您的帮助

1 个答案:

答案 0 :(得分:2)

更改此行:

Q1 = input ("If you could have any pet what would it be?")

是:

Q1 = input("If you could have any pet what would it be?").lower()

应该注意这一点:

if Q1 in "dog,cat,rabbit,gerbil,fish,rat":`

很奇怪,应该是

if Q1 in {"dog", "cat", "rabbit", "gerbil", "fish", "rat"}:

否则,dog,cat,rabbit将成为有效的宠物。