如何编辑变量的值或使用函数附加列表



所以我正在尝试构建一个21点游戏,并试图构建一个系统来记录玩家的分数。我想把卡片的价值加到玩家的分数上,但我遇到了一些问题。

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) 

我当前收到以下错误:UnboundLocalError:分配之前引用了本地变量"player_score">

您可以这样做,将player_score声明为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)

或者你可以这样做,这样你就可以避免让player_score成为global,因为它是作为一个参数传递给函数的,并根据函数的返回值更新:

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:使用类的示例,其中方法可以访问类属性,以便可以使用属性而不是全局变量。

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

最新更新