Python代码没有运行,我需要重新安装Python吗?

2024-04-29 07:35:00 发布

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

所以当我试着运行这个代码的时候。如您所见,它要求输入一个数字。你知道吗

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

Tags: numbermostis数字callintprintthan
2条回答

在语句print('Vini is older than '+guess)中,您试图将一个字符串('Vini早于')与一个整数(17)连接起来,这是错误的,因此您将得到一个错误。你知道吗

使用

 print('Vini is older than '+str(guess))

str(guess)将整数转换为字符串。 现在连接两个有效的字符串。你知道吗

如果需要将guess转换为字符串,则print语句可以工作,请尝试编写+ str(guess)而不是+ guess。你知道吗

相关问题 更多 >