") if age >= 24: print("you are getting old") print (age) els-6ren">
gpt4 book ai didi

python 3.3.2 年龄 >= 24 : TypeError: unorderable types: str() >= int()

转载 作者:行者123 更新时间:2023-11-28 19:49:32 36 4
gpt4 key购买 nike

print("how old are you")
age = input(">")
if age >= 24:
print("you are getting old")
print (age)
else:
print("i don't care")
print (age)

这是我遇到的错误:

if age >= 24:
TypeError: unorderable types: str() >= int()

最佳答案

在 Python 3 上,input() 总是 返回一个字符串值。使用 int() type转换它:

if int(age) >= 24:

字符串值和 int 不可排序:

>>> '24' > 23
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() > int()

请注意,如果无法转换输入,int() 会抛出一个ValueError 异常:

>>> int('Why do you want to know my age?')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Why do you want to know my age?'

关于 python 3.3.2 年龄 >= 24 : TypeError: unorderable types: str() >= int(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18262398/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com