好吧,这是我第一天使用python 3,我遇到了一个问题。我的脚本应该说如果输入的年龄超过13岁,你就可以玩游戏了,但它说即使你的年龄在13岁以下,你也足够大了。当年龄低于13岁时,控制台也应该关闭。
下面是我的脚本:
print("How old are you?")
age = int(input())
if age >= "13":
print("You are old enough to play! Let's get started!")
else:
print("You are not old enough.")
sys.exit("Oh dear")请帮帮忙,谢谢。
发布于 2013-10-19 03:15:12
您正在将字符串与整数进行比较:
age = int(input())
if age >= "13":在Python 2中,字符串总是大于数字。请改用数字:
if age >= 13:在Python 3中,将字符串与类似的整数进行比较会引发错误:
>>> 13 >= "13"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() >= str()给你一个更清晰的信息,告诉你你做错了什么。
发布于 2013-10-19 03:17:16
不能将整数与引号进行比较,编译器会将其识别为字符串而不是int。
if age >= 13:https://stackoverflow.com/questions/19457378
复制相似问题