我对编码还很陌生,我正在制作一款游戏,用户可以在其中对着计算机玩随机抽卡游戏。目标是首先达到 50 分,然后通过抽牌来达到。例如,2 个 Clubs 的值应该是 2,而 King of Hearts 的值应该是 13。但是,在我编辑代码时,它随机显示了一个我不熟悉的错误,也不知道如何修理。谁能帮我?我试图通过在 game_type 函数中添加“global user_score、random_drawn_card、computer_score”来修复它,这带来了另一条错误消息“SyntaxError: name 'user_score' is used before global declaration”。有人可以帮我吗?
# Useful Definitions
user_score = 0
computer_score = 0
user_turn_score = 0
computer_turn_score = 0
type = "user"
winner = "none"
import random
class Card:
suits = {'c': '♣','h': '♥','s': '♠','d': '♦'}
faces = {
1: 'Ace',
11: 'Jack',
12: 'Queen',
13: 'King'
}
def __init__(self, rank, suit):
'''Called when you create a new card. For example `Card(10, 'h')`'''
self.rank = rank
self.suit = suit
def __repr__(self):
'''A string representation of the card'''
face = self.faces.get(self.rank, self.rank)
return f'{face}{self.suits[self.suit]}'
def __add__(self, other):
'''What happens when you add a card to another (or to an integer)
For example this_card + someOther_card'''
if isinstance(other, int):
return self.rank + other
return self.rank + other.rank
def __radd__(self, other):
'''What happens when you add another or integer to this card.
For example `someOther_card + this_card`'''
if isinstance(other, int):
return other + self.rank
return self.rank + other.rank
sorted_deck = [Card(n, s) for s in Card.suits for n in range(1, 14)]
def game_turn(type):
while user_score != 50 and computer_score != 50:
global user_score, random_drawn_card, computer_score
if type == "user":
cont = input("It is your turn, " + user_name + ". Press enter to draw a random card.")
random_drawn_card = random.choice(sorted_deck)
print("Your card: " + str(random_drawn_card))
sorted_deck.remove(random_drawn_card)
if random_drawn_card == "Ace♣" or random_drawn_card == "Ace♥" or random_drawn_card == "Ace♦" or random_drawn_card == "Ace♠" or random_drawn_card == "Jack♣" or random_drawn_card == "Jack♥" or random_drawn_card == "Jack♦" or random_drawn_card == "Jack♠":
user_turn_score = 0
print("You drew an Ace or a Jack. You will not gain any points this turn.")
type = "computer"
else:
value = random_drawn_card.rank
user_turn_score = user_turn_score + value
print("Your points this round: " + str(user_turn_score))
turn_type = input("Do you want to continue your turn? (Y/N) ")
if turn_type == "Y":
type = "user"
else:
type = "computer"
user_score = user_score + user_turn_score
print("Your total score: " + user_score)
print()
print("***************************************************************")
print()
else:
computer_turn_score = 0
print("It is the computer's turn. Drawing a random card.")
random_drawn_card = random.choice(sorted_deck)
print("Computer's Card: " + str(random_drawn_card))
sorted_deck.remove(random_drawn_card)
if random_drawn_card == "Ace♣" or random_drawn_card == "Ace♥" or random_drawn_card == "Ace♦" or random_drawn_card == "Ace♠" or random_drawn_card == "Jack♣" or random_drawn_card == "Jack♥" or random_drawn_card == "Jack♦" or random_drawn_card == "Jack♠":
computer_turn_score = 0
print("The computer drew an Ace or a Jack. It will not gain any points this turn.")
type = "computer"
else:
value = random_drawn_card.rank
computer_turn_score = computer_turn_score + value
print("Computer's points this round: " + str(computer_turn_score))
computer_turn = random.randint(1,2)
if computer_turn == 1:
type = "computer"
print("The computer will take another turn.")
else:
type = "user"
computer_score = computer_score + computer_turn_score
print("The computer will not take another turn.")
print("Computer's total score: " + computer_score)
print()
print("***************************************************************")
print()
user = game_turn(type)
答案 0 :(得分:0)
我认为问题在于您在语句 global user_score
之前使用了变量 user_score。
如果你想使用这些全局变量,你应该移动函数开头的 global ...
语句(向上移动一行)。