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

Python计算玩家得分时出现int与list无法+=运算的报错排查

问题排查:TypeError: unsupported operand type(s) for +=: 'int' and 'list'

问题背景

我编写了用于计算指定玩家得分的calc_score函数:

def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in range(0, len(user_cards)):
            score += user_cards[int(card)]
    elif player == "comp":
        for card in range(0, len(comp_cards)):
            score += comp_cards[int(card)]
    else:
        return
    return score

调用该函数时触发如下错误:

line 27, in calc_score
    score += user_cards[int(card)]
TypeError: unsupported operand type(s) for +=: 'int' and 'list'

通过Thonny调试发现,类似['placeholder1', 'placeholder2'][0]的索引操作返回的是列表本身。我编写了看似等效的测试代码却能正常运行:

user_cards = [11,10]
comp_cards = [10,8]

score = 0
if "user" == "user":
    for card in range(0, len(user_cards)):
        score += user_cards[int(card)]
elif "user" == "comp":
    for card in range(0, len(comp_cards)):
        score += comp_cards[int(card)]
else:
    print()
print(score)

输出结果为21。检查变量和列表的值均无异常,无法定位问题,完整脚本如下:

import random

# Define Variables

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
user_cards = []
comp_cards = []
user_score = 0
comp_score = 0

# Define Functions

def give_card(num):
    """Returns random cards of a certain amount (num)"""
    num = int(num)
    cards_give = []
    for card in range(0, num):
        cards_give.append(random.choice(cards))
    return cards_give


def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in range(0, len(user_cards)):
            score += user_cards[int(card)]
    elif player == "comp":
        for card in range(0, len(comp_cards)):
            score += comp_cards[int(card)]
    else:
        return
    return score

user_cards.append(give_card(2))
comp_cards.append(give_card(2))
user_score = calc_score("user")
comp_score = calc_score("comp")

报错原因

核心问题出在卡片列表的初始化方式:

  • give_card(2)返回的是一个包含2张卡片的列表(比如[11, 10])
  • 使用user_cards.append(give_card(2))时,会把整个返回的列表作为单个元素添加到user_cards中,导致user_cards变成嵌套列表,结构为[[11, 10]]
  • 而测试代码中user_cards是直接赋值为一维列表[11,10],两者结构完全不同

在calc_score函数的循环中:

  • 嵌套列表user_cards的长度为1,循环仅执行1次,user_cards[int(card)]取到的是内部的子列表(比如[11,10])
  • 尝试将整数score与列表相加,就触发了TypeError

修复方案

方案1:替换append为extend

使用extend方法将give_card返回的列表元素逐个添加到玩家卡片列表中:

user_cards.extend(give_card(2))
comp_cards.extend(give_card(2))

方案2:直接赋值卡片列表

跳过空列表初始化,直接将give_card的返回值赋值给玩家卡片列表:

# 移除原有的 user_cards = [] 和 comp_cards = []
user_cards = give_card(2)
comp_cards = give_card(2)

优化建议:简化得分计算循环

无需通过索引遍历,直接遍历列表元素更简洁且不易出错:

def calc_score(player):
    """Calculates the score of the specified player (player)"""
    score = 0
    if player == "user":
        for card in user_cards:
            score += card
    elif player == "comp":
        for card in comp_cards:
            score += card
    else:
        return
    return score

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 01:37:35