Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';如果';语句未正确触发_Python_If Statement - Fatal编程技术网

Python';如果';语句未正确触发

Python';如果';语句未正确触发,python,if-statement,Python,If Statement,出于某种原因,无论用户输入是什么(Yes,no,“,eserw3),第一个if语句总是会被触发。关于为什么elif和else从未被激活,有什么见解吗?(下面的代码编译得非常完美,没有任何错误) 先谢谢你 def retry(): user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy)) if user_input == "Yes" or "yes":

出于某种原因,无论用户输入是什么(Yes,no,“,eserw3),第一个if语句总是会被触发。关于为什么elif和else从未被激活,有什么见解吗?(下面的代码编译得非常完美,没有任何错误)

先谢谢你

def retry():
        user_input = raw_input("Would you like to face %s again? (Yes/No)" % (Enemy))
        if user_input == "Yes" or "yes":
            respawn()
            getMove()
        elif user_input == "No" or "no":
            print "Thanks for playing!"
        else:
            print "Please enter either Yes or No."

将您的if条件更改为

user_input in ["Yes", "yes"]
原因:当您编写
用户输入==“Yes”或“Yes”
时,其计算结果为:

(user_input == "Yes") or "yes"

OR的第二部分是
True
always(非零长度字符串)。因此,如果总是执行块,则会出现
问题

更好的方法是:立即将输入小写<代码>用户输入=原始输入(…).lower()
。这样,就不需要在一个支管上重复或可能忘记下套管。通过这一改进,您可以随意编辑代码。
user_input in ["Yes", "yes"]
(user_input == "Yes") or "yes"