1

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!

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

4

You're trying to access the variable myshowingcard and other global variables inside the function explicitly stating it. Adding global myshowingcard and the like for the other variables will fix the problem, however, it will make your code harder to read in the future - it's easy to lose track of where things are getting modified and how they're actually being accessed.

You should try pass the arguments to the function instead:

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

And calling the function like this:

checkCards(myshowingcard, myhiddencard, theirshowingcard, theirhiddencard)

And maybe look at how scope works in python to understand the error.

stoksc
  • 324
  • 3
  • 12
0

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.

Dinko Pehar
  • 5,454
  • 4
  • 23
  • 57