所以当我试着运行这个代码的时候。如您所见,它要求输入一个数字。你知道吗
print('How old is Vini?')
number= 17
guess= int(input('enter a number'))
if guess== number:
print('Bravo Vini is 17')
elif guess < number:
print('Vini is older than '+guess)
elif guess > number:
print('Vini younger than '+guess)
如果这个数等于17,它就会执行,但是如果这个数更高或更低,它会给我这个错误:
当数字较高时:
Traceback (most recent call last):
File "C:\Users\ervin\Desktop\loja.py", line 9, in <module>
print('Vini younger than '+guess)
TypeError: must be str, not int
当数字较低时:
Traceback (most recent call last):
File "C:\Users\ervin\Desktop\loja.py", line 7, in <module>
print('Vini is older than '+guess)
TypeError: must be str, not int
在语句
print('Vini is older than '+guess)
中,您试图将一个字符串('Vini早于')与一个整数(17)连接起来,这是错误的,因此您将得到一个错误。你知道吗使用
str(guess)
将整数转换为字符串。 现在连接两个有效的字符串。你知道吗如果需要将
guess
转换为字符串,则print
语句可以工作,请尝试编写+ str(guess)
而不是+ guess
。你知道吗相关问题 更多 >
编程相关推荐