Python绝对初学者:第2章#单词问题

Python绝对初学者:第2章#单词问题,python,traceback,Python,Traceback,我已经开始为绝对初学者开发著名的Python 3e。我一直在忠实地复制代码,但有些人却不断收到错误消息。因此,我使用了help/examples文件夹中提供的代码,但仍然得到相同的消息: Traceback (most recent call last): File "word_problems.py", line 6, in <module> input("Press the enter key to find out.") File "<string>

我已经开始为绝对初学者开发著名的Python 3e。我一直在忠实地复制代码,但有些人却不断收到错误消息。因此,我使用了help/examples文件夹中提供的代码,但仍然得到相同的消息:

Traceback (most recent call last):
  File "word_problems.py", line 6, in <module>
    input("Press the enter key to find out.")
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

使用
raw\u input
代替
input

如果使用
输入
,则键入的数据将被解释为 Python表达式,这意味着您最终得到了gawd 目标变量中的对象类型是什么,并且范围很广 可以生成的异常范围。因此,您不应该使用
input
除非你把一些东西放进临时测试中,以供使用 只有对Python表达式略知一二的人才能使用

raw\u input
总是返回一个字符串,因为,见鬼,这就是你想要的 总是输入。。。但是你可以很容易地把它转换成特定的 键入所需的,并捕获可能发生的特定异常。 希望有了这样的解释,很容易就能知道你是谁 应该使用


|

您正在使用python2版本

因此,您可以使用raw_input()而不是input()

您的代码应该是:

print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
raw_input("Press the enter key to find out.")
print("2000 - 100 + 50 =", 2000 - 100 + 50)
如果您希望仅在空白输入或输入时显示输出,您可以将简单验证设置为:

print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
ur = raw_input("Press the enter key to find out.")

if ur== '':
    print "She weigh ", 2000 - 100 + 50,"pounds"
else:
    print 'SOrry!'

正如其他人所说,这是因为Python3中的
input
的行为与Python2中的
raw\u input
类似。因此,一种解决方案是使用
raw\u输入
使其在python 2中工作


但是,如果您正在阅读这本书,它似乎在使用python 3,我认为您最好现在就切换到python 3,您不应该在书中进一步讨论这些问题。

这是因为您使用的是
python 2
。尝试
raw\u input()
关键是,您应该升级Python版本;毫无疑问,您以后会遇到其他不一致的情况。更新到哪一个?我学习的是Python2.7,我有几门课程建议,由于他们使用的是旧的程序,所以我建议继续使用该版本。谢谢,我没有想到这一点!几年前我上了一门Python课程,但忘记了那个细节,我只是假设原始代码是正确的。谢谢
print("If a 2000 pound pregnant hippo gives birth to a 100 pound calf,")
print("but then eats 50 pounds of food, how much does she weigh?")
ur = raw_input("Press the enter key to find out.")

if ur== '':
    print "She weigh ", 2000 - 100 + 50,"pounds"
else:
    print 'SOrry!'