【发布时间】:2017-10-18 21:24:34
【问题描述】:
我正在做 Zed Shaw 的“Learn Python the Hard Way”第 3 版中的提问练习,我的代码如下所示:
print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
输出应该是这样的:
How old are you? 38
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're '38' old, '6\'2"' tall and '180lbs' heavy.
但是,因为我使用的是 Python 3,所以输出最初看起来像:
How old are you?
Traceback (most recent call last):
File "script.py", line 2, in <module>
age = raw_input()
NameError: name 'raw_input' is not defined
然后就像我用 input() 替换 raw_input() 一样:
How old are you?
Traceback (most recent call last):
File "script.py", line 2, in <module>
age = input()
EOFError: EOF when reading a line
【问题讨论】:
-
看起来像 Python 3 系统上的 Python 2 代码。另一方面,
print "How old are you?",似乎可以工作,这就是 Python 2 的语法。这里有点可疑。 -
print 应该是 print()。这是 Py2 和 Py3 之间的另一个区别