如果不起作用

2024-04-19 18:40:24 发布

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

我试着做一个简单的程序,让用户输入name和{},然后用if-else打印结果。在

我要等到20岁才有结果

Congratulation Edit your age is 20 and you can find the truth

但它不起作用这是我用的代码。在

编码

name = input("what is your name \n")
print('Nice to meet you', name, "!!")
age = input("Your Age ? \n")

if age > 18:
    print('Congratulation ' + name + ' your age is '+ str(age), ' and you can find the truth ')
else:
    print('Sorry ' + name + 'your age is '+ str(age), ' and you can also find the truth if you start from today')

以及运行代码后遇到的错误。在

错误

^{pr2}$

帮我解决这个问题。在


Tags: andthe代码nameyouageyourif
1条回答
网友
1楼 · 发布于 2024-04-19 18:40:24

阅读错误消息:

TypeError: unorderable types: str() > int()

您正在尝试比较字符串和整数。必须先将输入转换为int

^{pr2}$

这将允许你将它与数字进行比较。在

或者,在需要与数字比较时转换它

if int(age) > 18:

但这实际上是一个可用性问题。最好一得到它就把它转换成int,因为年龄的增长,你可能更需要数值而不是字符串值。在

相关问题 更多 >