-2

I've been trying to code a simple calculator on python GUI but I'm getting a syntax error message. I am new to programming so I am unsure what do.

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

Code

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()
Reti43
  • 9,656
  • 3
  • 28
  • 44

2 Answers2

2

if you are using python 2x use 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
+
'+'
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • 1
    Yup. To help OP it would probably help to replicate their error in that same context you are answering to help them visualize what is going on. Use `+`, for example as input instead of `hello` – idjaw Apr 03 '16 at 19:27
  • 1
    @idjaw updated, thanks – Hackaholic Apr 03 '16 at 19:33
  • Specifically, `input()` tries to evaluate the input as if it was a python expression. So passing `5+7` in the input will return 12. And just as in a normal script, just writing `+` is invalid syntax. – Reti43 Apr 03 '16 at 19:43
0

use raw_input() instead of input()

input() interprets the data you enter as a Python expression. raw_input() on the other hand returns the string you enter.

Vasanth
  • 1,238
  • 10
  • 14