'Raw_input() Python - 学习Python的艰难道路练习11'

2024-04-16 23:15:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做Zed Shaw的“艰难地学习Python”第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)

输出应如下所示:

^{pr2}$

但是,由于我使用的是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

Tags: youmostinputagerawlineoldare
2条回答

看起来你的代码是python2代码。这是一个使用python3语法的修订版。在

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))

对于python3,尝试将所有的“raw_input()”替换为“input()”,它已经替换了“raw_input()”

相关问题 更多 >