Skip to main content
 首页 » 编程设计

python之它说我定义的变量是 "referenced before assignment"

2025年05月04日25xiaohuochai

我正在尝试使用 python 3.4.3 制作一个简单的二十一点游戏。到目前为止,我已经介绍了一些选项,例如 split 或 ace 可以是 1 或 11。我得到的下一张牌让我破产然后它会说我破产但是如果我不破产那么下一个函数(housePlay())不起作用,你知道为什么吗? 这是错误:

Traceback (most recent call last): 
  File "F:\Computing\Black Jack.py", line 112, in <module> 
    housePlay() 
  File "F:\Computing\Black Jack.py", line 78, in housePlay 
    print("The house have a",c,"and a",d,"giving them a total of",houseCount) 
UnboundLocalError: local variable 'houseCount' referenced before assignment 

p.s 我对编码很陌生,所以请使用简单的术语,以便我能理解你

    import random 
playerCount=0 
houseCount=0 
cards={"Ace of Hearts":1, 
       "Two of Hearts":2, 
       "Three of Hearts":3, 
       "Four of Hearts":4, 
       "Five of Heats":5, 
       "Six of Hearts":6, 
       "Seven of Hearts":7, 
       "Eight of Hearts":8, 
       "Nine of Hearts":9, 
       "Ten of Hearts":10, 
       "Jack of Hearts":10, 
       "Queen of Hearts":10, 
       "King of Hearts":10, 
       "Ace of Diamonds":1, 
       "Two of Diamonds":2, 
       "Three of Diamonds":3, 
       "Four of Diamonds":4, 
       "Five of Diamonds":5, 
       "Six of Diamonds":6, 
       "Seven of Diamonds":7, 
       "Eight of Diamonds":8, 
       "Nine of Diamonds":9, 
       "Ten of Diamonds":10, 
       "Jack of Diamonds":10, 
       "Queen of Diamonds":10, 
       "King of Diamonds":10, 
       "Ace of Spades":1, 
       "Two of Spades":2, 
       "Three of Spades":3, 
       "Four of Spades":4, 
       "Five of Spades":5, 
       "Six of Spades":6, 
       "Seven of Spades":7, 
       "Eight of Spades":8, 
       "Nine of Spades":9, 
       "Ten of Spades":10, 
       "Jack of Spades":10, 
       "Queen of Spades":10, 
       "King of Spades":10, 
       "Ace of Clubs":1, 
       "Two of Clubs":2, 
       "Three of Clubs":3, 
       "Four of Clubs":4, 
       "Five of Clubs":5, 
       "Six of Clubs":6, 
       "Seven of Clubs":7, 
       "Eight of Clubs":8, 
       "Nine of Clubs":9, 
       "Ten of Clubs":10, 
       "Jack of Clubs":10, 
       "Queen of Clubs":10, 
       "King of Clubs":10} 
 
temp = [] 
for i in cards: 
    temp.append(i) 
a=random.choice(temp) 
b=random.choice(temp) 
c=random.choice(temp) 
d=random.choice(temp) 
f=random.choice(temp) 
while a == b: 
    b=random.choice(temp) 
while c == b or c== a: 
    c=random.choice(temp) 
while d == b or d== a or d==c: 
    d=random.choice(temp) 
while f == a or f == b or f == c or f == d: 
    e=random.choice(temp)    
playerCount+=cards[a] 
playerCount+=cards[b] 
houseCount+=cards[c] 
 
def housePlay(): 
    print("The house have a",c,"and a",d,"giving them a total of",houseCount) 
    if houseCount<17: 
        print("The house hits and gets a",f) 
        houseCount+=cards[f] 
        if houseCount>21: 
            print("The house go bust, you win") 
        else: 
            if playerCount>houseCount: 
                print("Unlucky, the house total is larger than yours so you lose") 
            elif playerCount==houseCount: 
                print("You have then same total as the house so you get your money back") 
            else: 
                print("Your total is larger than the house's so you win") 
    else: 
        print("The house stay with a total of",houseCount) 
        if playerCount>houseCount: 
            print("Unlucky, the house total is larger than yours so you lose") 
        elif playerCount==houseCount: 
            print("You have then same total as the house so you get your money back") 
        else: 
            print("Your total is larger than the house's so you win") 
 
print("Your cards:",a," and ",b,"Which add to ",playerCount) 
print("Dealers card:",c,"Which adds to ",houseCount) 
play=input("Would you like to Hit or Stand?") 
if play=="hit": 
    e=random.choice(temp) 
    while e == a or e== b or e==c or e==d or e == f: 
        e=random.choice(temp) 
    playerCount+=cards[e] 
    if playerCount>21: 
        print("You were dealt a",e) 
        print("Unlucky you have gone bust") 
    else: 
        housePlay() 
elif play=="stand": 
    houseCount+=cards[d] 
    if houseCount<17: 
        housePlay() 
    else: 
        if playerCount>houseCount: 
            print("Unlucky, the house total is larger than yours so you lose") 
        elif playerCount==houseCount: 
            print("You have then same total as the house so you get your money back") 
        else: 
            print("Your total is larger than the house's so you win") 

请您参考如下方法:

def housePlay(houseCount): 在函数定义中将 houseCount 作为参数传递

 housePlay(houseCount) 

像这样调用 housePlay 函数。