【问题标题】:SyntaxError: unexpected EOF while parsing input commandsSyntaxError:解析输入命令时出现意外的 EOF
【发布时间】:2016-01-14 21:54:54
【问题描述】:

今天我在写这个程序

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 tablessimp add 命令时。

我已经多次重新查看我的代码并阅读了一堆其他SyntaxError: unexpected EOF while parsing 线程,但仍然找不到我出错的地方。对不起,如果很明显我对这种东西很陌生。请帮忙!

【问题讨论】:

    标签: python input syntax


    【解决方案1】:

    您正在 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
    

    【讨论】:

    • 好的,谢谢!!我想我只是搞砸了 python 解释器,我以为它是在 python 3 上的。我得调查一下。
    【解决方案2】:

    如果您使用的是 Python 2.7(或任何早于 Python 3 的版本),您需要更改您的 input(...) 命令以使用 raw_input(...)。我进行了更改,此后它运行良好。

    【讨论】:

    • 不明白为什么有人反对这个答案,这似乎完全有道理
    • 我也不知道。但是,请注意,也有人对另一个答案投了反对票。如果你点击那里的“0”,你可以看到赞成票和反对票。
    • 有些人就是很奇怪。
    • 如果它困扰你;提供赞成票。继续讨论其他问题可能是更好地利用个人资源。 :-)
    【解决方案3】:

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

    (和第一个帖子说的差不多)

    【讨论】:

      猜你喜欢
      • 2013-04-25
      • 2011-07-15
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 2017-10-03
      • 2018-02-02
      相关资源
      最近更新 更多