简体   繁体   English

我的python计算器给出了无效的语法错误

[英]my python calculator gives an invalid syntax error

I can't get my calculator to work. 我无法使用计算器。 when I run it, it says that y elif statement is invallid, functions are above this clode block, it should work. 当我运行它时,它说y elif语句是无效的,函数在此clod块上方,它应该可以工作。

choice = raw_input("chose an operator [1,2,3,4]")
num1 = raw_input("input number 1")
num2 = raw_input("input number 2")

if choice == 1:
    print(num1,"+",num2,"=",add(num1,num2)

elif choice == 2:
    print(num1,"-",num2,"=",minus(num1,num2)

elif choice == 3:
    print(num1,"x",num2,"=",times(num1,num2)

elif choice == 4:
    print(num1,"/",num2,"=",divide(num1,num2)
else:
    print("that's not a valid operator")

and here is my error 这是我的错误

  File "calculator.py", line 27
    elif choice == 2:

您没有在除最后一个语句之外的所有打印语句中关闭)

You are missing a ) on each line 您在每一行上都缺​​少一个)

if choice == 1:
    print(num1,"+",num2,"=",add(num1,num2)   # <--- needs ) to close

Note: raw_input "asks" for a string value, in your case you should change 注意:raw_input“ asks”为字符串值,在这种情况下,您应该更改

if choice == 1:    

to

if choice == "1":

otherwise just use input 否则只需使用输入

ex: 例如:

choice = input("chose an operator [1,2,3,4]:\n")  # <- added \n, puts a new line at that point

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM