So, i've been working on a blackjack bot with python as a command prompt application, and im trying to run this code but i get an error saying,
"UnboundLocalError: local variable 'myshowingcard' referenced before assignment"
Here is the code i'm trying to run (Entire code):
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()
Originally I made the code giving each side cards a function called giveCards() but it wouldn't work. Please help me, thanks!
At beginning of your checkCards
function definition put like this:
def checkCards():
global myshowingcard, myhiddencard, theirshowingcard, theirhiddencard
if myshowingcard == myhiddencard:
myshowingcard = cards[random.randint(0, 51)]
... rest of the code
This way, function is using globally defined variables instead of local ones.