Question

So, my personal challenge is to make a simple Blackjack game in python. This is the way I've gone about doing it:

singlePlayerCard = random.choice(availableCards)
singlePlayerCard2 = random.choice(availableCards)

if(singlePlayerCard == [1,2,3,4,5,6,7,8,9,10]):
    singlePlayerCardValue = singlePlayerCard

if(singlePlayerCard2 == [1,2,3,4,5,6,7,8,9,10]):
    singlePlayerCard2Value = singlePlayerCard2


singlePlayerCardValueTotal = singlePlayerCardValue+singlePlayerCard2Value
print ("The total of your card values is %i"%singlePlayerCardValueTotal)

When it comes to print the singlePlayerCardValueTotal it tells me either that singlePlayerCardValue or singlePlayerCard2Value has been referenced before assignment. It's confused me slightly as I've had this issue in the past with another simple program I made but the structure is different here. Apart from the aforementioned issue, it was working fine. Also if you have the time, please inform me of any better solutions to some areas if necessary.

As always, thanks in advance! :)

Was it helpful?

Solution

As you noted in your comment, "referenced before assignment" means that you've mentioned a variable before it's been given a value.

It looks like your problem is this:

if(singlePlayerCard == [1,2,3,4,5,6,7,8,9,10]):
    singlePlayerCardValue = singlePlayerCard

Since your singlePlayerCard is never equal to the list [1,2,3,4,5,6,7,8,9,10], you will not assign a value to the variable when it's a number card.

The == operator is not the way to check whether a list contains a value. Try this, instead:

if(singlePlayerCard in [1,2,3,4,5,6,7,8,9,10]):
    singlePlayerCardValue = singlePlayerCard
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top