我正在做Zed Shaw的“以艰难的方式学习Python”第三版中的提问练习,我的代码如下:
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发布于 2017-10-19 05:36:41
看起来您拥有的代码是python 2代码。这是一个带有python 3语法的修订版。
Here是关于最后一行中的.format()调用的一些阅读。我认为.format方法更容易理解。
here是对Python3中的input()函数的一些解读
age = input("How old are you?")
height = input("How tall are you?")
weight = input("How much do you weigh?")
print("So, you're {} old, {} tall and {} heavy.".format(age, height, weight))发布于 2017-10-19 05:28:29
对于Python3,尝试将所有的"raw_input()“替换为简单的"input()”,它取代了"raw_input()“。
https://stackoverflow.com/questions/46819494
复制相似问题