gpt4 book ai didi

python - 艰难地学习 Python 练习 11

转载 作者:太空宇宙 更新时间:2023-11-04 07:03:38 24 4
gpt4 key购买 nike

我正在尝试通过艰难的方式学习 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)

关于python - 艰难地学习 Python 练习 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7045888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com