If statement 为什么这个If-Elif语句总是导致;是”;变量

If statement 为什么这个If-Elif语句总是导致;是”;变量,if-statement,If Statement,基本上,我编写此代码是为了询问用户是否想要玩球: print("Do you want to play ball?") answer = input("I will throw the ball and you will catch it! Yes or no?") if answer == 'yes' or 'Yes': { print("Great, lets play!") } elif answer == 'No' or 'no': { print("owwkayy

基本上,我编写此代码是为了询问用户是否想要玩球:

print("Do you want to play ball?")

answer = input("I will throw the ball and you will catch it! Yes or no?")


if answer == 'yes' or 'Yes': {
    print("Great, lets play!")
}
elif answer == 'No' or 'no': {
    print("owwkayyyy...")
}
然而,当它运行时,如果我说不,它仍然会打印程序“太好了,让我们玩吧”


我对编码是新手,我真的不明白为什么它不起作用。

这是因为您需要为第一个if语句修复您的条件。这样写是不正确的:

if answer == 'yes' or 'Yes':
你需要像这样重写答案:

if answer == "yes" or answer == "Yes":
对于您的elif语句也是如此。

使用(answer.lower()=“yes”)或(answer==“yes”或answer==“yes”)