使用python 2.7.5
x = (raw_input)'''What will you Reply?
a. Nod
b. Answer
c. Stare
'''))
if x == "Nod" or "a" or "nod":
print '''You nod shyly.
"Oh that's Great"'''
elif x == "Answer" or "b" or "answer":
print '''You answer in a low tone.
"You can speak!"'''
elif x == "Stare" or "c" or "stare":
print '''you stare blankly, so does the AI.
"Well okay then."'''
当我运行它时,无论我在提示中输入什么,它只会触发'''你害羞地点头。 “哦,太棒了”'''
但是如果我复制它并将其粘贴到我的python shell中,它有“哦”的问题,如果我摆脱它,它在“那是”的t有问题,如果我只是摆脱“那太棒了”它对下一个elif语句的前三个字符有问题。 WTF是错误的,我的python代码和shell最近工作正常,能够拆分if和elif。但现在突然间它只是不想。
答案 0 :(得分:5)
if x == "Nod" or "a" or "nod"
被解析为
if (x == "Nod") or ("a") or ("nod"):
"a"
是真实的,所以无论是否x == "Nod"
,条件始终为真。
相反,您可以使用:
if x in ("Nod", "a", "nod"):
答案 1 :(得分:1)
if x == "Nod" or "a" or "nod":
这将始终导致True
。
你应该使用
if x in ["Nod", "a", "nod"]
或
if x == "Nod" or x == "a" or x == "nod"
答案 2 :(得分:0)
你的第一个条件:
if x == "Nod" or "a" or "nod":
始终评估为true
。试试这段代码:
x = raw_input('What will you Reply? a. Nod b. Answer c. Star ')
if x in ["Nod", "a", "nod"]:
print '''You nod shyly. "Oh that's Great"'''
elif x in ["Answer", "b", "answer"]:
print '''You answer in a low tone. "You can speak!"'''
elif x in ["Stare", "c", "stare"]:
print '''you stare blankly, so does the AI. "Well okay then."'''