SyntaxError:解析输入命令时出现意外的EOF

时间:2016-01-14 21:54:54

标签: python syntax

今天我正在编写这个程序

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()

但是,我一直收到此错误

 SyntaxError: unexpected EOF while parsing

或更具体地说

    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

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

当我尝试在输入中输入mult tables或simp add命令时。

我多次重新查看我的代码,并在解析线程时读取了一堆其他的SyntaxError:意外的EOF,但仍无法找到我出错的地方。对不起,如果它显然我对这种东西很新。请帮忙!

3 个答案:

答案 0 :(得分:2)

您正在Python 2下运行您的代码,其input()函数返回将eval()函数应用于您输入的字符串的结果。我相信如果您使用raw_input()函数,错误将会消失。这只会返回您输入的字符串。请参阅下文了解更多详情。

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

答案 1 :(得分:0)

如果您使用的是Python 2.7(或早于Python 3的任何内容),则需要更改输入(...)命令以使用 raw_input(...)< / strong>即可。我做了那个改变,之后一切正常。

答案 2 :(得分:0)

在Python 2中,您正在使用input()函数。您可以

  • 使用Python 3或更高版本

    • 更新您的编辑器(您是否正在使用标准编辑器或Visual Studio或其他工具?)
    • 下载新的编辑器,例如标准Python 3+Anaconda
  • 如果使用的是Python 2,请使用raw_input()函数( 将在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'

(类似于第一篇文章所说的