pythonlistfunctioninteger

Why doesn't it just give the list itself?


I had this function

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

and when it was called it gave me this error:

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

I debugged it in thonny and it turns out it said that ['placeholder1', 'placeholder2'][0] just gave the list itself.

I even tested it with seemingly equivalent code and it worked.

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)

Output: 21

I checked the variables and lists' values and they were normal too.

I couldn't find anything wrong.

Lastly here is the whole script (as of posting this answer):

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")

Solution

  • Your problem is here:

    user_cards.append(give_card(2))
    

    user_cards is a list, and give_card(2) returns another list of two numbers. Appending a list to a list makes the appended list itself be an element of the resulting list:

    list = [1, 2]
    list.append([3,4])
    print(list) # prints [1, 2, [3, 4]]
    

    What you need instead is

    user_cards.extend(give_card(2))
    

    or

    user_cards += give_card(2)
    

    This will produce what you require in user_cards because the extend function or the += operator appends each element of the given list.

    By the way, instead of iterating through indexes of a list, it's more readable to iterate through its elements:

        for card in user_cards:
            score += card
    

    But even better would be to use the sum function:

        score = sum(user_cards)