1

Today I was writing this program

from random import randint

def practice():
    command = input("Welcome to math practice! Type mult tables to practice multiplication tables, or simp add for single digit addition")
    if command == "mult tables":
        while True:
            first_value_x = randint(2, 12)
            second_value_x = randint(2, 12)
            number_x = int(input("%s x %s" % (first_value_x, second_value_x )))
            if number_x == first_value_x * second_value_x:
                print("Correct!!")
            else:
                print("You did not get the answer correct.")
    elif command == "simp add":
        while True:
            first_value_simp_add = randint(1,9)
            second_value_simp_add = randint(1,9)
            number_simple_add = int(input("What is %s + %s" %(first_value_simp_add, second_value_simp_add)))
            if number_simple_add == first_value_simp_add + second_value_simp_add:
                print("Well done!")
            else:
                print("You did not the answer correct")
    else:
        print("The command you entered does not exist. Please retype a command")
        practice()

practice()

However, I keep getting this error

SyntaxError: unexpected EOF while parsing

or more specifically

Traceback (most recent call last):
  File "/Users/student/Desktop/math practice.py", line 49, in <module>
    practice()
  File "/Users/student/Desktop/math practice.py", line 6, in practice
    command = input("Welcome to math practice! Type mult tables to     practice multiplication tables, or simp add for single digit addition")
  File "<string>", line 1
    simp add
      ^
SyntaxError: unexpected EOF while parsing

or

Traceback (most recent call last):
  File "/Users/student/Desktop/math practice.py", line 49, in <module>
    practice()
  File "/Users/student/Desktop/math practice.py", line 6, in practice
    command = input("Welcome to math practice! Type mult tables to practice multiplication tables, or simp add for single digit addition")
  File "<string>", line 1
    mult tables
         ^
SyntaxError: unexpected EOF while parsing

When I try to enter the mult tables or simp add commands in the input.

I have relooked over my code many times and read a bunch of other SyntaxError: unexpected EOF while parsing threads, yet still cannot find where I went wrong. Sorry if its obvious I'm very new to this kind of stuff. Please help!

wjandrea
  • 28,235
  • 9
  • 60
  • 81
iamthebacon
  • 21
  • 1
  • 5

3 Answers3

2

You are running your code under Python 2, whose input() function returns the result of applying the eval() function to the string you enter. I believe the errors will disappear if you instead use the raw_input() function. this simply returns the string you enter. See below for more detail.

>>> input("Value: ")
Value: 3
3
>>> k = 42
>>> input("Value: ")
Value: k
42
>>> raw_input("Value: ")
Value: k
'k'
>>> input("Value: ")
Value: some random string
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    some random string
              ^
SyntaxError: invalid syntax
holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Ok thank you!! I guess I just screwed up the python interpreter somehow I assumed it was on python 3. Ill have to look into that. – iamthebacon Jan 15 '16 at 00:10
0

If you're using Python 2.7 (or anything earlier than Python 3), you need to change your input(...) command to use raw_input(...). I made that change, and it works fine thereafter.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

In Python 2, you are using the input() function. You can either

  • Use Python 3 or higher

    • Update your editor (Are you using the standard editor or visual studio or what else?)
    • Download a new editor such as standard Python 3+ or Anaconda
  • use the raw_input() function if you are using Python 2 (will not work in Python 3+):

>>> input('Enter a value: ') #Basic input function
Enter a value: 3
3
>>> print('That worked fine.')
That worked fine
>>> var = 42
>>> input('Var: ') '''Will give the value of that variable:
       basically runs print(input) on the input'''
Var: var
42
>>> raw_input('Var: ')
Var: var
'var'

(It is similar to what the first post said)

AksLolCoding
  • 157
  • 10