TypeError:必须是str,而不是int Python3 issu

2024-04-25 15:09:03 发布

您现在位置:Python中文网/ 问答频道 /正文

线路怎么了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)

我总是出错:

^{pr2}$

你们一般怎么调试?我是编程新手。有没有办法知道它到底想把绳子放在哪里?在


Tags: nameinputyourtodaydatedifffuturehi
2条回答

future是一个整数,因此不能与字符串连接。您可以这样做:

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

或将int转换为str

^{pr2}$

示例:

^{3}$

输出:

C:\Users\Documents>py test.py
Hi, please enter your name job
Hi please enter your DOB 1876
Hi please enter today's date 2018
Hi jobin the year of  2023 you will turn 147

示例:

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 ' + str(future) + 'you will turn ' + diff)

输出:

Hi, please enter your name job
Hi please enter your DOB 1876
Hi please enter today's date 2018
Hi jobin the year of 2023you will turn 147

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

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

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

相关问题 更多 >