我刚开始学习Python,只是学习一些基础知识。你能帮我纠正一下吗,告诉我我是怎么错的,为什么错了?
age = input('Please enter your age:')
ten = 10
agePlusTen= age + ten
print('You will be', agePlusTen, 'in 10 years'
Traceback (most recent call last):
File "C:/Users/Admin/Documents/Python/6.2 fixed.py", line 3, in <module>
print('You will be', age + ten, 'in 10 years')
TypeError: must be str, not int发布于 2017-11-08 00:24:10
在python中,input()返回字符串。因此,您应该首先将age转换为int,然后将其添加到变量ten中。
下列各点应能发挥作用。
age = input('Please enter your age:')
ten = 10
agePlusTen= int(age) + ten
print('You will be ' + str(agePlusTen) + ' in 10 years')此外,当您希望通过连接一组字符串来打印字符串时,如果其中任何字符串不是字符串,请不要忘记使用str()函数将它们转换为字符串。
发布于 2017-11-08 15:08:49
在python中,使用时总是返回一个字符串。也就是说,当使用input()存储变量时,变量将始终是字符串类型,从而将年龄变量转换为整数写入,如下所示:
age = input('Please enter your age:') ten = 10 agePlusTen= int(age) + ten print('You will be', agePlusTen, 'in 10 years'),
我觉得这个应该管用。
https://stackoverflow.com/questions/47169556
复制相似问题