Why is user_score and computer_score not being added to? (i think)

Question:

So I’m, coding a blackJack game,and I made a list called user_score and computer_score. I used the random module to choose a random int from a list called cards. But when I use .append() to add the random choice from cards, it doesn’t appear to be adding the random card to user_card / computer_card? Here is where I define it, and where I use the random module:

import random

user_score = 0
computer_score = 0

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

user_cards = []
computer_cards = []



def deal_card():
cards_left = 2
while not cards_left == 0:
    random_user = random.choice(cards)
    random_computer = random.choice(cards)
    user_cards.append(random_user)
    computer_cards.append(random_computer)
    cards_left -= 1
    print(user_score, computer_score)

and finally, this is where I call the function:

deal_card()
calculate_score(card_list=[user_score, computer_score])

calculate_score is defined here:

def calculate_score(card_list):
user_score = sum(user_cards)
computer_score = sum(computer_cards)

if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
    computer_score = 0
elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
    user_score = 0  
if user_cards.count(11) > 0:
    cards.remove(11)
    cards.append(1)
elif computer_cards.count(11) > 0:
    cards.remove(11)
    cards.append(1)
return user_score

PS: I’m still learning python, so please don’t go to advanced

Asked By: SmallComputer12

||

Answers:

In Python, any variable that you set in a function is a separate local variable in that function. To access the global variable instead, you have to write a global statement like this:

def calculate_score(card_list):
    global user_score, computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)
    ...

If you don’t set a variable but only read it, it uses the global one, which is why you can access user_cards and computer_cards inside the function. cards.remove does not actually change the cards variable but just reads it to see which list to edit, and then edits that list, which is why it accesses the global cards.

Answered By: user253751

You are not updating the global variables user_score and computer_score. To fix this issue, you need to declare these variables as global within the calculate_score function. Following is the updated calculate_score function:

def calculate_score():
    global user_score
    global computer_score
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0  
    if user_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)

Also, call the function without passing any arguments. Since you are using the global variables I don’t think you need to pass any arguments to calculate_score function.

deal_card()
calculate_score()
Answered By: Bilesh Ganguly

The issue is that the variables user_cards and computer_cards are not in the scope of deal_card() or calculate_score(). As mentioned in the previous answers, one way to deal with this problem is by using global variables. Another way to solve this, would be by passing and returning variables in your functions:

import random

# Pass cards variable to function
def deal_card(cards):
    cards_left = 2
    user_cards = []
    computer_cards = []
    while not cards_left == 0:
        random_user = random.choice(cards)
        random_computer = random.choice(cards)
        user_cards.append(random_user)
        computer_cards.append(random_computer)
        cards_left -= 1
        print(user_score, computer_score)
    # return user_cards and computer_cards array
    return user_cards, computer_cards

# Pass user_cards and computer_cards array to function
def calculate_score(user_cards, computer_cards):
    user_score = sum(user_cards)
    computer_score = sum(computer_cards)

    if computer_cards.count(11) > 0 and computer_cards.count(10) > 0:
        computer_score = 0
    elif user_cards.count(11) > 0 and user_cards.count(10) > 0:
        user_score = 0
    if user_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    elif computer_cards.count(11) > 0:
        cards.remove(11)
        cards.append(1)
    # return user_score and computer_score
    return user_score, computer_score

cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

# call deal_card with cards array
user_cards, computer_cards = deal_card(cards)

# call calculate_score with user_cards and computer_cards returned from deal_cards
user_score, computer_score = calculate_score(user_cards, computer_cards)
Answered By: Marcelo Paco
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.