Python“其他”语句始终返回True

时间:2018-12-06 10:45:14

标签: python new-operator

My Code

print("Hello World")
print("How're you?")
mood = input()
print("I'm " + mood + " too")
print("What's your name?")
name = input()
print(name + ". I really like that name!")
food = input("I really like pizza. Do you? Y/N ")
if food == 'y' or 'Y':
    print("Wow! " + name + ", you and I have a really similar taste in food, don't we? So what else do you like?")
elif food == 'n' or 'N':
    print("Oh, that's a shame. I was gonna treat us to a cheeky Papa Johns!")
else:
    print("Oh! Okay. Then what else do you like?")
other = input()
if other == 'liquorice' or 'Liquorice' or 'Brussels Sprouts' or 'brussels sprouts':
    print("Blegh! " + name + ", I respect you and all... but gross!")

Output

当我键入Y或y以外的其他内容时(代表是),我试图使程序使用ELIF和ELSE输出。我该怎么做才能解决此错误?

1 个答案:

答案 0 :(得分:0)

使用时

if food =='y' or 'Y':

python将一直返回true,您可以对此进行测试:

if 'false':
    print('true')

要使其正常工作,请将food =='y' or 'Y'替换为food =='y' or food=='Y'或@Peter Wood建议,请使用food的下/上