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

标签 python

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/

相关文章:

python - 什么Python模式可以用于并行化?

python - 错误 : When subclassing the `Model` class, 你应该实现一个 `call` 方法。关于 tensorflow 自定义模型

python:比较列表中的字符串

python - 使用颜色条时 get_position() 会做奇怪的事情

python - pandas:如何在一列上合并具有相同列名的多个数据框?

Python 对对象的引用以节省额外的输入

python - 导致 MultiIndex 中的级别不被删除的 Pandas 幕后发生了什么?

Python 将日期和时间转换为 pandas 索引

python - 使用 BeautifulSoup 解析 html 表格

python - Django - 脆皮表格可以分成两列吗?