【发布时间】:2014-03-23 02:18:14
【问题描述】:
您好,这是我在此代码上的第二篇文章,我没有收到错误,但 if else 语句无法按照我想要的方式运行 if(calculate[2] == 操作: 代码不起作用,即使我有 - 或 + 也不会注册。任何想法为什么?它给了我回复Sorry invalid text3。
calculate = input("Enter the problem in the format x + y = z ")
opperations = ["+", "-", "*", "/"]
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
space = " "
a = calculate[0]
a = int(a)
b = calculate[4]
b = int(b)
def opperation():
if opperations == "+":
A = 1
elif opperations == "-":
A = 2
elif opperations == "*":
A = 3
elif opperations == "/":
A = 4
if calculate[0] in numbers:
if len(calculate) > 1:
if calculate[2] == opperations:
if calculate[1] == space:
if A == 1:
c = a + b
print (c)
elif A == 2:
c = a - b
print (c)
elif A == 3:
c = a * b
print(c)
elif A == 4:
c = a / b
print(c)
else:
print("Sorry invalid text5")
else:
print("Sorry invalid text4")
else:
print("Sorry invalid text3")
else:
print("Sorry invalid text2")
else:
print("Sorry invalid text1")
【问题讨论】:
-
我猜你想使用
calculate[2] in opperations而不是你现在拥有的。 -
除非您特别询问如何解决跨版本兼容性问题(在这种情况下,您的问题显然应该描述该问题),否则您不应混合使用python-2.7 和python-3.x 标签。我暂时将它们都删除了。
标签: python