2016-04-03 20 views
-1

我一直在嘗試在python GUI上編寫一個簡單的計算器,但我收到了語法錯誤消息。我是編程新手,所以我不確定是什麼。使用輸入讀取數據時的語法錯誤()

Traceback (most recent call last): 
    File "C:\Users\kmart3223\Desktop\Martinez_K_Lab1.py", line 126, in <module> 
    main() 
    File "C:\Users\kmart3223\Desktop\Martinez_K_Lab1.py", line 111, in main 
    operation = input("What operations should we do (+, -, /, *):") 
    File "<string>", line 1 
    + 
    ^
SyntaxError: unexpected EOF while parsing 

代碼

def main(): 
    operation = input("What operations should we do (+, -, /, *):") 
    if(operation != '+' and operation != '-' and operation != '/' and operation != '*'): 
     print ("chose an operation") 
    else: 
     variable1 = int(input("Enter digits")) 
     variable2 = int(input("Enter other digits")) 
     if (operation == "+"): 
      print (add(variable1, variable2)) 
     elif (operation == "-"): 
      print (sub(variable1, variable2)) 
     elif (operaion == "*"): 
      print (mul(variable1, variable2)) 
     else: 
      print (div(variable1, variable2)) 
main() 
+1

的Python 2或Python 3? – erip

+0

您正在使用Python 2.使用'raw_input'而不是'input' – idjaw

+0

關閉原因Typo,以某種方式解決.... – Drew

回答

0

代替input()

input()使用raw_input()解釋你作爲一個Python表達式中輸入的數據。另一方面,raw_input()返回您輸入的字符串。

2

如果你使用python 2X使用raw_input()

>>> input()   # only takes python expression 
>>> input() 
+ 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1 
    + 
    ^
SyntaxError: unexpected EOF while parsing 
>>> input() 
'+'     # string ok 
'+' 
>>> input() 
7     # integer ok 
7 
>>> raw_input()    # Takes input as string 
+ 
'+' 
+1

是的。爲了幫助OP,它可能有助於在您回答相同的上下文中複製它們的錯誤,以幫助他們查看正在發生的事情。使用'+'作爲輸入,而不是'hello' – idjaw

+1

@idjaw已更新,謝謝 – Hackaholic

+0

具體來說,'input()'試圖評估輸入,就好像它是一個python表達式。因此,在輸入中傳遞'5 + 7'將返回12.正如在普通腳本中一樣,只是寫'+'是無效的語法。 – Reti43

相關問題