gpt4 book ai didi

python - 在 python 的 raw_input 中使用 %r

转载 作者:太空狗 更新时间:2023-10-30 02:22:12 27 4
gpt4 key购买 nike

在 python 的 raw_input 中使用 %r 是否可行?

关于上下文,我正在研究 Zed Shaw 的 Exercise 12 . (很好的资源!类(class)非常有帮助,节奏也很好。)

我在玩额外的学分,试图让 raw_input 重复我输入的内容。我知道我可以用打印语句完成同样的事情,但我很好奇我是否可以在 raw_input 中完成它。

我输入的代码是:

from sys import argv

script, firstname, lastname = argv

age = raw_input("Hello %r, what is your age? ") % firstname

print ("Ahh, Mr. %r, you are %r years old.") % (lastname, age)

我得到的错误是:

Traceback (most recent call last):
File "ex13a.py", line 5, in <module>
age = raw_input("Hello %r, what is your age? ") % firstname
TypeError: not all arguments converted during string formatting

提前致谢!

最佳答案

你的行应该是

raw_input("Hello %r, what is your age? " % firstname)

代替

raw_input("Hello %r, what is your age? ") % firstname

否则,您不会格式化您的 "Hello %r, ..." 字符串,而是格式化调用 raw_input 的结果字符串。

关于python - 在 python 的 raw_input 中使用 %r,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845238/

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