【问题标题】:im trying to add the average of something to a list, but im getting a NoneType error我试图将某些东西的平均值添加到列表中,但我得到一个 NoneType 错误
【发布时间】:2026-01-19 20:45:01
【问题描述】:

我想将变量 'average' 添加到名为 avgList 的列表中,但我收到错误消息 'NoneType' object has no attribute 'append'

tempList = []
nameList = []
avgList = []
ctr = 0
ctrr = 0
while ctrr <12:
    name = raw_input("Enter team name: ")
    ctrr += 1
    ctr = 0
    while ctr <8:
        score = input("Enter Scores: ")
        ctr += 1
        tempList.append(score)
    summ = sum(tempList)
    average = summ/len(tempList)
    avgList = avgList.append(int(average))
print max(avgList)

【问题讨论】:

    标签: python list append nonetype


    【解决方案1】:

    这一行是你的问题:

    avgList = avgList.append(int(average))
    

    append() 返回None,所以在第一次循环之后,avgList 不再是你的列表,而是None

    为避免这种情况,请勿将返回值分配回avgList。只是:

    avgList.append(int(average))
    

    您之前在脚本中正确执行了此操作。

    【讨论】:

      【解决方案2】:

      这是由这一行引起的:

      avgList = avgList.append(int(average))
      

      改成这样:

      avgList.append(int(average))
      

      【讨论】:

        最近更新 更多