catShietBud
catShietBud

Reputation: 27

Why do I get a tuple as a return value when I want a list?

For my first project, I'm trying to make blackjack. I want to be able to return the user_cards and computer_cards in a list to then use them in the next function to check for blackjack (21). For some reason I get this error code as its returning those values as a tuple, I think?

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from black_jack_game import *
  File "/home/runner/blackjack-start/black_jack_game.py", line 52, in <module>
    calculate_score(dealt_cards)
  File "/home/runner/blackjack-start/black_jack_game.py", line 43, in calculate_score
    if user_card[item] == 11:
TypeError: list indices must be integers or slices, not tuple

code :

import random

# dealing the cards to the user
def deal_card():
    cards = [11,2,3,4,5,6,7,8,9,10,10,10,10] * 4 #  a full 52 card deck
    user_cards = []
    computer_cards = []

    #getting a random card 
    for i in range(0,3):
        user_cards.append(random.choice(cards))
        computer_cards.append(random.choice(cards))
    
    # this will remove the cards that have ben used
    for i in user_cards:
        if i in cards:
            cards.remove(i)

    for i in computer_cards:
        if i in cards:
            cards.remove(i)

    # this returns a list
    return [user_cards,computer_cards]

dealt_cards = deal_card()


# adding the score up of those cards 
def calculate_score(func):

    #spliting the list that returned from the last function
    user_card = dealt_cards[0]
    computer_card = dealt_cards[1]
    
    #getting the sum
    user_card_sum = sum(user_card)
    computer_card_sum = sum(computer_card)

    # check for an 11 (ace)
    # over 21, remove the 11 and replace it with a 1
    for item in enumerate(user_card):
        if user_card[item] == 11:
            user_card[item] = 1
    
    # a hand with only 2 cards: ace + 10) and return 0 
    if user_card_sum == 21 | computer_card_sum == 21:
        return 0

    print(user_card,user_card_sum)

calculate_score(dealt_cards)

Upvotes: 1

Views: 1109

Answers (3)

Akshar
Akshar

Reputation: 21

enumerate() function which you have used returns 2 values (index, item) and you have written only item hence it is giving you this error.

Try changing your code to:

for index, item in enumerate(user_card):
        if user_card[index] == 11:
            user_card[index] = 1

Upvotes: 2

catShietBud
catShietBud

Reputation: 27

ok, so I fixed it as soon as I posted:

for item in range(len(user_card)):
    if user_card[item] == 11:
        user_card[item] = 1

Upvotes: 0

Mahan Vyakti
Mahan Vyakti

Reputation: 129

You have used enumerate() method in the for loop

enumerate() method adds counter to an iterable and returns it. The returned object is a enumerate object. Basically it returns (index, element) pair for every element in array.

So, you might try to fix this by changing your code to:

for index, item in enumerate(user_card):
        if user_card[index] == 11:
            user_card[index] = 1

or simply:

for idx in range(len(user_card)):
    if user_card[idx] == 11:
        user_card[idx] = 1

Upvotes: 1

Related Questions