Python运行报错TypeError:_Printer与int实例不支持'>='运算修复求助
报错原因解释
TypeError: '>=' not supported between instances of '_Printer' and 'int' 的含义是:>= 比较运算符不支持在 _Printer 类型的实例和整数(int)之间执行计算。
代码问题根源
你当前代码存在缩进逻辑错误:
- 你原本的业务逻辑应该是「平均成绩≥70分且缺勤次数≤5次的前提下,再根据学分判断特权等级」,但你现在把学分判断的代码块错误放到了缺勤次数>5次的else分支里。
- 当程序走缺勤超标的else分支时,
credits变量根本没有被赋值,如果你之前的运行环境有过credits = print这类错误赋值(print内置函数的类型就是_Printer),运行到credits >=17的判断时就会触发本次报错。 - 哪怕没有之前的错误赋值,这时候也会触发变量未定义的NameError,本质都是缩进错误导致的逻辑问题。
修复后的代码
如果需要保留原英文提示,只需要调整学分判断逻辑的缩进位置即可:
av_grade = int(input("What is your average grade? ")) if av_grade >= 70: absences = int(input("How many absences do you have? ")) if absences <= 5: credits = int(input("How many credits have you earned? ")) # 学分判断逻辑需要放在缺勤合格的分支内 if credits >= 17: print("You are eligible for senior privileges") elif credits >= 11: print("You are eligible for junior privileges") else: print("You are not eligible for privileges") else: print("You are not eligible for privileges") else: print("You are not eligible for privileges")
如果需要中文提示版本可以使用以下代码:
av_grade = int(input("你的平均成绩是多少? ")) if av_grade >= 70: absences = int(input("你缺勤了多少次? ")) if absences <= 5: credits = int(input("你已经获得了多少学分? ")) if credits >= 17: print("你符合高级特权申请条件") elif credits >= 11: print("你符合中级特权申请条件") else: print("你不符合特权申请条件") else: print("你不符合特权申请条件") else: print("你不符合特权申请条件")
内容的提问来源于stack exchange,提问作者Shourya Ravula
相关产品推荐
相关产品推荐

