gpt4 book ai didi

Python 二十一点 UnboundLocalError

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:50 25 4
gpt4 key购买 nike

<分区>

所以,我一直在使用 python 作为命令提示符应用程序开发一个 blackjack 机器人,我试图运行这段代码,但我收到一条错误消息,
“UnboundLocalError:赋值前引用的局部变量‘myshowingcard’”

这是我要运行的代码(完整代码):

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() 的函数,但它不起作用。请帮助我,谢谢!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com