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

Its saying the variable i have defined was "referenced before assignment"

我正在尝试使用 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")

现在,只需在 housePlay() 函数中执行 global houseCount - 这应该会让您渡过难关。

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

 housePlay(houseCount)

像这样调用housePlay函数。

在函数的开头添加 global houseCount housePlay()

解释:正如评论中所说,您试图查看的变量 houseCount 在被赋值之前未在函数中找到。您可以毫无问题地使用其他变量(ad),因为它们没有在函数中被修改。
当您在函数中更改 houseCount 的值时,您必须将其声明为全局变量,以便函数知道该变量已在全局范围内定义。

在 Python 中编码时要遵循的一个好的设计实践是尽可能地分解成小的、独立的函数。这就是您开始使用 housePlay 函数所做的事情。不幸的是,对于您和您的代码,Python 也有 范围规则 。也就是说,在函数内定义的变量是该函数的局部变量(从函数外部看不到),从函数内调用的变量首先在封闭函数内查找(L),然后是任何封闭函数 (E),然后是模块文件或全局文件的顶层 (G),然后是构建-在 (B) 中。在此处查看最佳答案 - Short Description of the Scoping Rules?

在你的例子中,你只有一个 python 脚本,而不是模块。模块在同一目录中有一个关联的 __init__.py 文件。因此,您在文件顶层定义的变量(playerCounthouseCount 等)在每个函数中都将不可见。 (在L中找不到,没有E,你没有模块,也没有定义为全局的( G)。这就是你 "variable referenced before assignment" 错误的原因。

最好将函数所需的任何对象(变量)作为函数 arguments:

传递给函数
def housePlay(playerCount, houseCount):
    ....some code here....

您需要将此函数调用为:

housePlay(playerCount, houseCount)

或者,您可以将每个顶级变量定义为 global:

global playerCount = 0
global houseCount = 0

可能更好的选择是使用 if __name__ == '__main__': construct.This 看起来像这样:

import random

def main():
    playerCount = 0
    houseCount = 0
    ...rest of your code...

if __name__ == '__main__':
    main()

您的所有代码都包含在 main() 函数中(尽管您通常会在 main 旁边定义其他函数),如果您是直接 运行 脚本而不是将它作为另一个脚本的一部分导入(这就是 if __name__ == '__main__' 所做的)它只会 运行 main() 函数中的所有内容。

这样做将确保任何内部函数(例如 housePlay)都可以看到在封闭 (E) 作用域中定义的变量(在本例中, main 函数).