我想要的是制作我的代码,这样如果我进入"你是"加上一个补充词(在列表中)和/或另一个词,如"你是一个很好的机器人"它将打印出来:"谢谢!"
这是我的代码:
complements = ["nice","happy","good","smart","wonderful"]
def chat():
input = raw_input("You: ")
if input in "You are a ":
if input in complements:
print "TIM: Thank you"
else:
print "I don't understand"
chat();
无论我做什么,它都会自动进入else语句
答案 0 :(得分:0)
您有两个主要问题:
if input in "You are a ":
测试整个输入是否在"You are a "
中,而不是该短语是否在输入的开头;和if input in complements
测试整个输入是否在您的列表中,而不是列表中的一个项目是否在输入中。尝试类似:
def chat(compliments=["nice", "happy", "good", "smart", "wonderful"]):
input = raw_input("YOU: ")
if input.startswith("You are a "):
if any(input[10:].startswith(c) for c in compliments):
print "TIM: Thank you"
这给了我:
>>> chat()
YOU: You are a nice robot
TIM: Thank you