You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Python输入判断代码时持续报错,请求排查原因

问题分析与解决
  • 错误根源:input()函数获取的用户输入默认是字符串类型,直接拿字符串和整数1000做大小比较,Python不支持不同类型的数值比较,会触发TypeError报错。

修正后的基础代码:

score = input("what you score bro: ")
# 将输入的字符串转换为整数类型
score_num = int(score)
if score_num > 1000:
    print("winner")
else:
    print("loser")

进阶优化(处理非数字输入):
如果担心输入不是数字导致报错,可以加异常处理避免程序崩溃:

score = input("what you score bro: ")
try:
    score_num = int(score)
    if score_num > 1000:
        print("winner")
    else:
        print("loser")
except ValueError:
    print("请输入有效的数字哦")

内容的提问来源于stack exchange,提问作者Stef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 16:45:25