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!

0

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

4 Comments

Don't understand why somebody has downvoted this answer, which seems perfectly justifiable
I don't know, either. However, note that someone down-voted the other answer, also. If you click on the '0' there, you can see both up- and down-votes.
Some people are just weird.
If it bugs you; supply up-votes. Moving on to other issues is likely a better use of personal resources. :-)
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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.