关于文本:Python Blackjack UnboundLocalError

Python Blackjack UnboundLocalError

本问题已经有最佳答案,请猛点这里访问。

所以,我一直在研究一个使用python作为命令提示应用程序的21点bot,我试图运行这段代码,但我得到一个错误消息说,
"UnboundLocalError:局部变量'myShowingCard'在赋值之前被引用"

这是我尝试运行的代码(整个代码):
BR/>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import random

 # Defining list of cards
cards = ["Ace of Spades","Two of Spades","Three of Spades","Four of Spades",
"Five of Spades","Six of Spades","Seven of Spades","Eight of Spades",
"Nine of Spades","Ten of Spades","Jack of Spades","Queen of Spades",
"King of Spades","Ace of Clubs","Two of Clubs","Three of Clubs",
"Four of Clubs","Five of Clubs","Six of Clubs","Seven of Clubs",
"Eight of Clubs","Nine of Clubs","Ten of Clubs","Jack of Clubs",
"Queen of Clubs","King of Clubs","Ace of Hearts","Two of Hearts",
"Three of Hearts","Four of Hearts","Five of Hearts","Six of Hearts",
"Seven of Hearts","Eight of Hearts","Nine of Hearts","Ten of Hearts",
"Jack of Hearts","Queen of Hearts","King of Hearts","Ace of Diamonds",
"Two of Diamonds","Three of Diamonds","Four of Diamonds","Five of Diamonds",
"Six of Diamonds","Seven of Diamonds","Eight of Diamonds","Nine of Diamonds",
"Ten of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds"]

 # Giving both sides different cards
myshowingcard = cards[random.randint(0, 51)]
myhiddencard = cards[random.randint(0, 51)]
theirshowingcard = cards[random.randint(0, 51)]
theirhiddencard = cards[random.randint(0, 51)]
mysum = 0
theirsum = 0

 # Function to check if and cards are the same
def checkCards():
    if myshowingcard == myhiddencard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()
    if myshowingcard == theirhiddencard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()
    if myshowingcard == theirshowingcard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()
    if myhiddencard == theirhiddencard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()
    if myhiddencard == theirshowingcard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()
    if theirhiddencard == theirshowingcard:
        myshowingcard = cards[random.randint(0, 51)]
        myhiddencard = cards[random.randint(0, 51)]
        theirshowingcard = cards[random.randint(0, 51)]
        theirhiddencard = cards[random.randint(0, 51)]
        checkCards()


def printCards():
    print("Your cards are {} and {}.".format(myshowingcard, myhiddencard))
    print("Your opponent's showing card is {}.".format(theirshowingcard))


checkCards()
printCards()

最初,我编写了一个代码,给每一张卡片赋予一个名为givecards()的函数,但它不起作用。请帮帮我,谢谢!


您试图访问变量myshowingcard和显式声明它的函数内的其他全局变量。为其他变量添加global myshowingcard等可以解决这个问题,但是,这将使您的代码在将来更难读取——很容易丢失对修改内容的位置以及实际访问方式的跟踪。

您应该尝试将参数传递给函数:

1
2
 # Function to check if and cards are the same
 def checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard):

像这样调用函数:

1
checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard)

也许可以看看范围如何在Python中工作来理解错误。


在您的checkCards函数定义的开头,如下所示:

1
2
3
4
5
def checkCards():
    global myshowingcard, myhiddencard, theirshowingcard, theirhiddencard
    if myshowingcard == myhiddencard:
        myshowingcard = cards[random.randint(0, 51)]
        ... rest of the code

这样,函数使用全局定义的变量而不是局部变量。