好吧,我有以下代码执行我不希望它做的事情.如果你运行该程序,它会问你"你好吗?" (显然),但是当你回答适用于elif语句的问题时,我仍然会得到一个if语句响应.为什么是这样?
talk = raw_input("How are you?")
if "good" or "fine" in talk:
print "Glad to here it..."
elif "bad" or "sad" or "terrible" in talk:
print "I'm sorry to hear that!"
Run Code Online (Sandbox Code Playgroud)
问题是or操作员在这里没有做你想做的事.你真正说的是if the value of "good" is True or "fine" is in talk."good"的值始终为True,因为它是非空字符串,这就是为什么该分支总是被执行的原因.
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |