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

如何修复Python程序TypeError: 'int' object is not iterable报错

报错原因

  • 你的代码中number变量仅存储最后一次输入的单个整数,而sum()、max()等方法要求传入可迭代对象(如列表),直接传入单个int就会触发'int' object is not iterable的类型错误
  • 此外currLowest函数中print("The current lowest is: ", min(num)行遗漏了右括号,存在语法错误

无列表实现方案

因为要求尽量不使用列表,我们可以用独立变量存储计算所需的核心状态,不需要留存所有输入的数字:

# 定义全局状态变量,也可以放在main函数里作为局部变量传递
total_sum = 0
count = 0
max_num = None
min_num = None
quit_flag = False

def main():
    global total_sum, count, max_num, min_num, quit_flag
    while not quit_flag:
        choice = getSelection()
        if choice == 'G': # 输入数字
            getANumber()
        elif choice == 'S': # 计算总和
            currSum()
        elif choice == 'A': # 计算平均值
            currAvg()
        elif choice == 'H': # 取最大值
            currHighest()
        elif choice == 'L': # 取最小值
            currLowest()
        elif choice == 'Q': # 退出
            print('Thanks for using the program!')
            quit_flag = True
            pause()
        else:
            print('Invalid selection. Pick only from the menu.')
            pause()


def displayMenu():
    print("\n"*50, "=== The Menu ===")
    print("G] Get a number")
    print("S] Display current sum")
    print("A] Display current average")
    print("H] Display the current highest number")
    print("L] Display the current lowest number")
    print("Q] Quit")
    print("\n" * 2)

def getSelection():
    displayMenu()
    result = input("Enter your selection: ").strip().upper()
    return result

def getANumber():
    global total_sum, count, max_num, min_num
    num = int(input("Enter a number, please: "))
    total_sum += num
    count += 1
    # 更新最大值最小值
    if max_num is None or num > max_num:
        max_num = num
    if min_num is None or num < min_num:
        min_num = num

def currSum():
    global count, total_sum
    if count == 0:
        print("Go back and enter a number first...")
        pause()
        return 
    print("The current sum is: ", total_sum)
    pause()

def currAvg():
    global count, total_sum
    if count == 0:
        print("Go back and enter a number first...")
        pause()
        return
    avg = total_sum / count
    print("The current avgerage is: ", round(avg,2) )
    pause()

def currHighest():
    global count, max_num
    if count == 0:
        print("Go back and enter a number first...")
        pause()
        return
    print("The current highest is: ", max_num)
    pause()

def currLowest():
    global count, min_num
    if count == 0:
        print("Go back and enter a number first...")
        pause()
        return
    print("The current lowest is: ", min_num)
    pause()

def pause():
    input("\npress any key to continue....\n")

main()

修改说明

  • 新增4个状态变量分别记录累计总和、输入数字个数、最大值、最小值,每次输入新数字时直接更新对应变量,无需存储所有历史数字
  • 所有计算函数直接读取状态变量输出结果,不需要调用sum、max等依赖可迭代对象的方法,完全避开列表使用
  • 额外给输入的选项加了大写转换,避免用户输入小写字母报无效选项的问题
  • 修复了原代码中currLowest函数的语法错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 07:24:04