在我的 Python 代码中(我无法将其显示为图像,因为 rep<10),我试图制作一个程序,询问用户的姓名,然后猜测他们的年龄从 1 到 30。出于某种原因,以“AgeQ ="(L16) 开头的行不起作用,因为它“无法将‘int’对象隐式转换为 str”。以下是代码中的错误和错误日志等,将不胜感激。
AgeQ = 'Are you ' + Age[x] + 'years old? (Y/N)'
Traceback (most recent call last):
File "\\rhs-vmw-p-fs01\students\2010\R07972\Python\test2.py", line 16, in <module>
AgeQ = 'Are you ' + Age[x] + 'years old? (Y/N)'
TypeError: Can't convert 'int' object to str implicitly
最佳答案
您可以将 int 显式转换为字符串:
AgeQ = 'Are you ' + str(Age[x]) + 'years old? (Y/N)'
或者,更好的是,使用字符串格式:
AgeQ = 'Are you %s years old? (Y/N)' % Age[x]
关于python - 在输入中连接的列表和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22357632/