首页 > 解决方案 > TypeError:必须是 str,而不是 int Python3 问题

问题描述

第6行有什么问题:

 name = (input('Hi, please enter your name '))
dob = int(input('Hi please enter your DOB '))
today = int(input("Hi please enter today's date "))
future = int(today + 5)
diff = str(future - dob)
print ('Hi ' + name + 'in the year of ' + future + 'you will turn ' + diff)

我不断收到错误:

TypeError: must be str, not int

大家一般是怎么调试的?我是编程新手。有没有办法确切地知道它想要字符串的位置?

标签: pythonpython-3.xdebugging

解决方案


Python 无法自动将整数变量转换为字符串。

您需要将future变量显式转换为str如下类型:

print ('Hi ' + name + 'in the year of ' + str(future) + 'you will turn ' + diff)

推荐阅读