Skip to main content
 首页 » 编程设计

Python 不会跳过 "if [strg] or [strg] in variable"

2025年05月04日57xiaohuochai

<分区>

我正在用 Python 2.7 编写一个简单的基于文本的游戏。在下面的代码中,即使用户回答的内容与“运行”或“无”完全不同,arrested() 函数仍然会启动。我很确定问题出在这一行代码:

if "run" or "nothing" in answer: 

因为如果我只提供一个字符串,如下:

if "run" in answer: 

程序运行良好,如果答案不包含“运行”,则调用 remote_spot()。整个代码如下:

def facing_cops(): 
    print "There are several cars that might serve as stake-outs for the cops. What do you do?" 
    answer = raw_input("> ") 
    if "run" or "nothing" in answer: 
        arrested("Cops got suspicious. You are arrested, along with the drug dealer.") 
    else: 
        remote_spot() 

你认为这是怎么回事?

谢谢,

克里斯