0 python if-statement python-2.7 python-3.x
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
Run Code Online (Sandbox Code Playgroud)
无论我做什么,Addition if语句是唯一运行的东西,所以如果我把2和1它总是等于3,即使我放了 - .我该怎么做才能解决这个问题?
if test == "Addition" or "Add" or "Adding" or "+"
Run Code Online (Sandbox Code Playgroud)
被解释为
if (test == "Addition") or "Add" or "Adding" or "+"
Run Code Online (Sandbox Code Playgroud)
总是评估True
为非空字符串的真值True
所以要修复,你应该这样做:
if test in ("Addition", "Add", "Adding", "+")
Run Code Online (Sandbox Code Playgroud)
您可能还需要考虑,test.lower()
以便您可以考虑您正在测试的单词的各种情况.
像这样:
if test.lower() in ("addition", "add", "adding", "+")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42 次 |
最近记录: |