【问题标题】:Error input output while excuting Python"执行 Python 时输入输出错误\"
【发布时间】:2022-12-18 09:40:01
【问题描述】:

我用 Python 编写了以下程序,但出现此错误 请指导我

age= (input("please inter your age:" ))
if age > 40:
    print ("old")
elif 40>age>=30:
    print ("middle-age")    
elif 30>age>=20:
    print ("yong")
TypeError: '>' not supported between instances of 'str' and 'int'

我检查了几次程序

【问题讨论】:

标签: python python-3.x python-2.7 syntax


【解决方案1】:

在进行比较时将年龄类型更改为int,因为您使用返回字符串的input 输入年龄。

age= (input("please inter your age:" ))
if int(age) > 40:
    print ("old")
elif 40>int(age)>=30:
    print ("middle-age")    
elif 30>int(age)>=20:
    print ("yong")

这给你你想要的预期输出

输出:

please inter your age:45
old

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    相关资源
    最近更新 更多