使用if语句时输入数字会报错:
TypeError: ‘>=’ not supported between instances of ‘str’ and ‘int’
这是因为input输入的是字符串,我们需要把字符串转换为int类型
例:
age = int(input('请输入年龄:'))
if age >= 18:
print("我已经成年了")
else:
print("未成年不能进入")
本文讲解了在使用Python的input函数时常见的类型错误:'>='不支持在'str'和'int'实例间操作。通过示例展示了如何将用户输入的字符串转换为整数,避免类型错误。
使用if语句时输入数字会报错:
TypeError: ‘>=’ not supported between instances of ‘str’ and ‘int’
这是因为input输入的是字符串,我们需要把字符串转换为int类型
例:
age = int(input('请输入年龄:'))
if age >= 18:
print("我已经成年了")
else:
print("未成年不能进入")

被折叠的 条评论
为什么被折叠?
