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

Python SyntaxError:分析输入命令时出现意外的EOF,python,input,syntax,Python,Input,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:

今天我在写这个节目

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
回溯(最近一次呼叫最后一次):
文件“/Users/student/Desktop/math practice.py”,第49行,在
做法()
文件“/Users/student/Desktop/math practice.py”,第6行,实践中
command=input(“欢迎进行数学练习!键入mult tables练习乘法表,或键入simp add练习单位数加法”)
文件“”,第1行
简单添加
^
SyntaxError:分析时出现意外的EOF

回溯(最近一次呼叫最后一次):
文件“/Users/student/Desktop/math practice.py”,第49行,在
做法()
文件“/Users/student/Desktop/math practice.py”,第6行,实践中
command=input(“欢迎进行数学练习!键入mult tables练习乘法表,或键入simp add练习单位数加法”)
文件“”,第1行
多张桌子
^
SyntaxError:分析时出现意外的EOF
当我尝试在输入中输入
mult tables
simp add
命令时


我已经多次重新阅读我的代码,并阅读了大量其他
语法错误:在分析
线程时出现意外的EOF,但仍然找不到哪里出了问题。很抱歉,很明显我对这类东西很陌生。请帮忙

如果您使用的是Python2.7(或Python3之前的任何版本),则需要将输入(…)命令更改为使用原始输入(…)。我做了这个更改,之后效果很好。

如果您使用的是Python2.7(或Python3之前的版本),则需要将输入(…)命令更改为使用原始输入(…)。我做了这个更改,之后效果很好。

您正在Python2下运行代码,Python2的
input()
函数返回将
eval()
函数应用于您输入的字符串的结果。我相信如果您改为使用
raw\u 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
>>输入(“值:”)
价值:3
3.
>>>k=42
>>>输入(“值:”)
值:k
42
>>>原始输入(“值:”)
值:k
“k”
>>>输入(“值:”)
值:一些随机字符串
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第1行
一些随机字符串
^
SyntaxError:无效语法

您正在Python2下运行代码,Python2的
input()
函数返回对输入的字符串应用
eval()
函数的结果。我相信如果您改为使用
raw\u 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
>>输入(“值:”)
价值:3
3.
>>>k=42
>>>输入(“值:”)
值:k
42
>>>原始输入(“值:”)
值:k
“k”
>>>输入(“值:”)
值:一些随机字符串
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第1行
一些随机字符串
^
SyntaxError:无效语法

在Python 2中,您使用的是
input()
函数。你也可以

  • 使用Python 3或更高版本

    • 更新您的编辑器(您使用的是标准编辑器还是visual studio或其他什么?)
    • 下载新编辑器,如标准或
  • 如果您使用的是Python 2(will不适用于Python 3+),请使用
    raw\u input()
    函数:


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

在Python2中,您使用的是
input()
函数。你也可以

  • 使用Python 3或更高版本

    • 更新您的编辑器(您使用的是标准编辑器还是visual studio或其他什么?)
    • 下载新编辑器,如标准或
  • 如果您使用的是Python 2(will不适用于Python 3+),请使用
    raw\u input()
    函数:


(类似于第一篇帖子所说的)

好的,谢谢!!我想我只是把python解释器搞砸了,我以为它是在python 3上的。我得调查一下。好的,谢谢!!我想我只是把python解释器搞砸了,我以为它是在python 3上的。我得调查一下。我不明白为什么有人否决了这个答案,这似乎是完全合理的。我也不知道。但是,请注意,也有人否决了另一个答案。如果你点击那里的“0”,你可以看到赞成票和反对票。有些人很奇怪。如果这让你感到厌烦;提供选票。继续讨论其他问题可能会更好地利用个人资源。:-)我不明白为什么有人否决了这个答案,这似乎是完全合理的,我也不知道。但是,请注意,也有人否决了另一个答案。如果你点击那里的“0”,你可以看到赞成票和反对票。有些人很奇怪。如果这让你感到厌烦;提供选票。继续讨论其他问题可能会更好地利用个人资源。:-)
>>> 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'