SyntaxError:解析输入命令时出现意外的EOF [英] SyntaxError: unexpected EOF while parsing input commands

查看:522
本文介绍了SyntaxError:解析输入命令时出现意外的EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我正在编写此程序

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

或更具体地说

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命令时.

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

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

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!

推荐答案

您正在Python 2下运行代码,该Python 2的input()函数返回将eval()函数应用于您输入的字符串的结果.我相信,如果您改为使用raw_input()函数,错误将消失.这只是返回您输入的字符串.有关更多详细信息,请参见下文.

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

这篇关于SyntaxError:解析输入命令时出现意外的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆