- 此问题在此处已有答案**:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?(6个答案)
昨天关门了。
当我输入第7行or
时,代码没有按预期工作。有人能告诉我原因吗?
# x = int(input("Insert price for your product :"))
x=100
print ("Does your product include taxes?")
# answer = input(" yes or no ")
answer = "no"
if answer == "yes" or "Yes" or "YES":
print ("final price is : ", x, "\n the tax is :" ,x*0.17)
elif answer == "no":
print ("final price is : ", x*1.17, "\n the taxes is ", x*0.17)
else:
print ("Your answer is not according to the dictionnary - try again")
我希望对单词YES的任何输入都能对代码中的if
起作用。
3条答案
按热度按时间yeotifhr1#
试试看:
而不是。
zsohkypk2#
而不是使用
if answer == "yes" or "Yes" or "YES":
.你应该试试if answer == "yes" or answer == "Yes" or answer == "YES":
.或者使用python内置的方法
lower()
,这样就可以将输入小写5sxhfpxr3#
这是语法上的问题。
在Python中,在这个上下文中,每次使用什么变量时,都需要详细说明,例如:
也可以采用其他格式,例如:
这个解决方案将把
answer
全部改为小写,这允许人们也写一些东西,比如YeS或yeS。2这也使代码看起来不那么混乱。