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

Python调用totalPoint函数if判断报错:function与int不支持>比较

问题原因与修复方案

报错'>' not supported between instances of 'function' and 'int'的含义是:你正在用函数对象和整数做大小比较,这是Python不支持的操作。

现存问题

  • 函数作用域逻辑错误:你调用weight()、scoreEarned()、scoreShift()后没有接收返回值,函数内部的局部变量无法被其他函数直接访问。你在totalPoint里写的scoreShift、scoreEarned实际指向的是同名函数对象,不是用户输入的数值,因此做大小比较时触发了类型错误。
  • scoreShift函数逻辑不全:当用户输入2(不需要调分)时,函数没有显式返回值,默认会返回None,后续计算会出现二次报错。
  • totalPoint没有定义入参:无法接收计算总分需要的得分、调分值等基础数据。
    特别注意不要让函数名和变量名重名,否则会覆盖原有函数的指向,引发意料之外的类型错误

修复后完整代码

# 提示语
print("This program reads exam/homework scores and reports your overall course grade.")
print()
print("Midterm 1:")

# 获取权重
def get_weight():
    weight = int(input("Weight (0-100)? "))
    return weight

# 获取得分
def get_score_earned():
    score = int(input("Score earned? "))
    return score

# 获取调分值
def get_score_shift():
    yes_or_no = int(input("Were scores shifted (1=yes, 2=no)? "))
    if yes_or_no == 1:
        shift_amount = int(input("Shift amount? "))
        return shift_amount
    # 不需要调分时返回0,简化后续判断逻辑
    return 0

# 计算并打印总分
def print_total_point(score_earned, score_shift):
    out_of = 100
    total = score_earned + score_shift
    # 限制总分不超过满分100
    if total > 100:
        total = 100
    print(f"Total points = {total}/{out_of}")

# 调用函数时接收返回值存储为变量
weight_val = get_weight()
score_val = get_score_earned()
shift_val = get_score_shift()
# 将需要的参数传入计算总分的函数
print_total_point(score_val, shift_val)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 13:36:04