'How do I edit the value of a variable or append a list using a function?
So I'm trying to build a blackjack game and I'm trying to build a system to record the player's score. I'd like to add the value of the card to the player's score, but I'm encountering some problems.
players_hand = []
player_score = 0
def play_the_round():
card_list_select = cards_list[random.randint(0,len(cards_list)-1)] #this picks a card
if card_list_select == "Ace":
ace_input = int(input("You have gotten an Ace! What value do you want your ace to be, 1 or 11: "))
card_value = ace_input
if not card_list_select == "Ace":
card_value = cards_value_dict[card_list_select] #this extracts the card's value from a dictionary
player_score += int(card_value)
players_hand.append(card_list_select)
Im currently getting this error: UnboundLocalError: local variable 'player_score' referenced before assignment
Solution 1:[1]
You can do something like this, declaring player_score as global:
import random
cards_list = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
cards_value_dict = dict(zip(cards_list, [1,2,3,4,5,6,7,8,9,10,10,10,10]))
players_hand = []
global player_score
player_score = 0
def play_the_round():
global player_score
card_list_select = cards_list[random.randint(0,len(cards_list)-1)] #this picks a card
if card_list_select == "Ace":
ace_input = int(input("You have gotten an Ace! What value do you want your ace to be, 1 or 11: "))
card_value = ace_input
if not card_list_select == "Ace":
card_value = cards_value_dict[card_list_select] #this extracts the card's value from a dictionary
player_score += int(card_value)
players_hand.append(card_list_select)
play_the_round()
print(players_hand, player_score)
... or you can do something like this which will allow you to avoid making player_score global since it is passed to the function as an argument and updated based on the function's return value:
players_hand = []
player_score = 0
def play_the_round(score):
card_list_select = cards_list[random.randint(0,len(cards_list)-1)] #this picks a card
if card_list_select == "Ace":
ace_input = int(input("You have gotten an Ace! What value do you want your ace to be, 1 or 11: "))
card_value = ace_input
if not card_list_select == "Ace":
card_value = cards_value_dict[card_list_select] #this extracts the card's value from a dictionary
score += int(card_value)
players_hand.append(card_list_select)
return score
player_score = play_the_round(player_score)
print(players_hand, player_score)
UPDATE: example of using a class where methods have access to class attributes, so that attributes can be used instead of global variables.
import random
class Blackjack:
def __init__(self):
self.cards_list = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King']
self.cards_value_dict = dict(zip(self.cards_list, [1,2,3,4,5,6,7,8,9,10,10,10,10]))
self.players_hand = []
self.player_score = 0
def play_the_round(self):
card_list_select = self.cards_list[random.randint(0,len(self.cards_list)-1)] #this picks a card
if card_list_select == "Ace":
ace_input = int(input("You have gotten an Ace! What value do you want your ace to be, 1 or 11: "))
card_value = ace_input
if not card_list_select == "Ace":
card_value = self.cards_value_dict[card_list_select] #this extracts the card's value from a dictionary
self.player_score += int(card_value)
self.players_hand.append(card_list_select)
game = Blackjack()
game.play_the_round()
game.play_the_round()
while game.player_score < 21:
more_cards = int(input(f"You have {len(game.players_hand)} cards with a score of {game.player_score}. Another card (1 for one more card, 0 for no more cards)?"))
if more_cards > 0:
game.play_the_round()
else:
break
print(game.players_hand, game.player_score, f"that's a busted hand" if game.player_score > 21 else f"you're still in the game")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
