if语句是唯一运行的东西,即使if语句没有任何==

时间:2015-11-10 01:03:42

标签: python python-2.7 python-3.x if-statement

test = str(raw_input("Which function would you like to use? "))



print test

one = int(raw_input("Input the first number. "))
two = int(raw_input("Input the second number. "))

if test == "Addition" or "Add" or "Adding" or "+":
    print one + two

elif test == "Subtraction" or "Subtract" or "Subtracting" or "-":
    print one - two

无论我做什么,Addition if语句是唯一运行的东西,所以如果我把2和1它总是等于3,即使我放了 - 。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:6)

if test == "Addition" or "Add" or "Adding" or "+"

被解释为

if (test == "Addition") or "Add" or "Adding" or "+"

始终评估为True,因为非空字符串的真值始终为True

所以要修复,你应该这样做:

if test in ("Addition", "Add", "Adding", "+")

您可能还想考虑test.lower(),以便您可以考虑所测试单词的各种情况。

像这样:

if test.lower() in ("addition", "add", "adding", "+")