我正在尝试通过艰难的方式学习 Python 的练习 11,但遇到了一些问题。以下是我使用 geedit 输入到 .py 文件中的内容(在 PC 上处理)
print "How old are you?",
age = raw_input('27')
print "How tall are you?",
height = raw_input('5\'8"')
print "How much do you weigh?",
weight = raw_input('180lbs')
print "So, you're %r old, %r tall and %r heavy." %(age, height, weight)
我无法让 %r 显示原始输入,它们往往在最后一行以 '' 形式出现,我做错了什么?
raw_input
's argument用作提示的文本。
你可以这样做:
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")
print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
如果你想提供默认值,那么你可以这样做:
age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")
age = age if age is not '' else '27'
height = height if height is not '' else '5\'8"'
weight = weight if weight is not '' else '180lbs'
print "So, you're %r old, %r tall and %r heavy." % (age, height, weight)
我是一名优秀的程序员,十分优秀!