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/