Python 二十一点 UnboundLocalError
Python Blackjack UnboundLocalError
所以,我一直在使用 python 作为命令提示符应用程序开发 21 点机器人,我正在尝试 运行 此代码,但我收到一条错误消息,
"UnboundLocalError: local variable 'myshowingcard' referenced before assignment"
这是我正在尝试的代码 运行 (完整代码):
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() 的函数,但它不起作用。请帮助我,谢谢!
在你的 checkCards
函数定义的开头这样写:
def checkCards():
global myshowingcard, myhiddencard, theirshowingcard, theirhiddencard
if myshowingcard == myhiddencard:
myshowingcard = cards[random.randint(0, 51)]
... rest of the code
这样,函数使用全局定义的变量而不是局部变量。
您正在尝试访问变量 myshowingcard
和函数中明确声明它的其他全局变量。为其他变量添加 global myshowingcard
等将解决问题,但是,这会使您的代码将来更难阅读 - 很容易忘记修改的位置以及它们的实际修改方式已访问。
您应该尝试将参数传递给函数:
# Function to check if and cards are the same
def checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard):
并像这样调用函数:
checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard)
也许可以查看 scope 在 python 中的工作原理以了解错误。
所以,我一直在使用 python 作为命令提示符应用程序开发 21 点机器人,我正在尝试 运行 此代码,但我收到一条错误消息,
"UnboundLocalError: local variable 'myshowingcard' referenced before assignment"
这是我正在尝试的代码 运行 (完整代码):
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() 的函数,但它不起作用。请帮助我,谢谢!
在你的 checkCards
函数定义的开头这样写:
def checkCards():
global myshowingcard, myhiddencard, theirshowingcard, theirhiddencard
if myshowingcard == myhiddencard:
myshowingcard = cards[random.randint(0, 51)]
... rest of the code
这样,函数使用全局定义的变量而不是局部变量。
您正在尝试访问变量 myshowingcard
和函数中明确声明它的其他全局变量。为其他变量添加 global myshowingcard
等将解决问题,但是,这会使您的代码将来更难阅读 - 很容易忘记修改的位置以及它们的实际修改方式已访问。
您应该尝试将参数传递给函数:
# Function to check if and cards are the same
def checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard):
并像这样调用函数:
checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard)
也许可以查看 scope 在 python 中的工作原理以了解错误。