I'm trying to make a simple blackjack game using python 3.4.3. So far i have introduced options such as split or the fact the an ace can be either a 1 or 11. I get to the problem where after the program has told me my cards and the houses cards and i say i want to hit, if the next card i get gets me bust then it will say that i go bust however if i don't go bust then the next function(housePlay()) doesn't work, do you know why? This is the error:
Traceback (most recent call last):
File "F:\Computing\Black Jack.py", line 112, in <module>
housePlay()
File "F:\Computing\Black Jack.py", line 78, in housePlay
print("The house have a",c,"and a",d,"giving them a total of",houseCount)
UnboundLocalError: local variable 'houseCount' referenced before assignment
p.s I'm quite new to coding so please use simple terms so that i can understand you
import random
playerCount=0
houseCount=0
cards={"Ace of Hearts":1,
"Two of Hearts":2,
"Three of Hearts":3,
"Four of Hearts":4,
"Five of Heats":5,
"Six of Hearts":6,
"Seven of Hearts":7,
"Eight of Hearts":8,
"Nine of Hearts":9,
"Ten of Hearts":10,
"Jack of Hearts":10,
"Queen of Hearts":10,
"King of Hearts":10,
"Ace of Diamonds":1,
"Two of Diamonds":2,
"Three of Diamonds":3,
"Four of Diamonds":4,
"Five of Diamonds":5,
"Six of Diamonds":6,
"Seven of Diamonds":7,
"Eight of Diamonds":8,
"Nine of Diamonds":9,
"Ten of Diamonds":10,
"Jack of Diamonds":10,
"Queen of Diamonds":10,
"King of Diamonds":10,
"Ace of Spades":1,
"Two of Spades":2,
"Three of Spades":3,
"Four of Spades":4,
"Five of Spades":5,
"Six of Spades":6,
"Seven of Spades":7,
"Eight of Spades":8,
"Nine of Spades":9,
"Ten of Spades":10,
"Jack of Spades":10,
"Queen of Spades":10,
"King of Spades":10,
"Ace of Clubs":1,
"Two of Clubs":2,
"Three of Clubs":3,
"Four of Clubs":4,
"Five of Clubs":5,
"Six of Clubs":6,
"Seven of Clubs":7,
"Eight of Clubs":8,
"Nine of Clubs":9,
"Ten of Clubs":10,
"Jack of Clubs":10,
"Queen of Clubs":10,
"King of Clubs":10}
temp = []
for i in cards:
temp.append(i)
a=random.choice(temp)
b=random.choice(temp)
c=random.choice(temp)
d=random.choice(temp)
f=random.choice(temp)
while a == b:
b=random.choice(temp)
while c == b or c== a:
c=random.choice(temp)
while d == b or d== a or d==c:
d=random.choice(temp)
while f == a or f == b or f == c or f == d:
e=random.choice(temp)
playerCount+=cards[a]
playerCount+=cards[b]
houseCount+=cards[c]
def housePlay():
print("The house have a",c,"and a",d,"giving them a total of",houseCount)
if houseCount<17:
print("The house hits and gets a",f)
houseCount+=cards[f]
if houseCount>21:
print("The house go bust, you win")
else:
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")
else:
print("The house stay with a total of",houseCount)
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")
print("Your cards:",a," and ",b,"Which add to ",playerCount)
print("Dealers card:",c,"Which adds to ",houseCount)
play=input("Would you like to Hit or Stand?")
if play=="hit":
e=random.choice(temp)
while e == a or e== b or e==c or e==d or e == f:
e=random.choice(temp)
playerCount+=cards[e]
if playerCount>21:
print("You were dealt a",e)
print("Unlucky you have gone bust")
else:
housePlay()
elif play=="stand":
houseCount+=cards[d]
if houseCount<17:
housePlay()
else:
if playerCount>houseCount:
print("Unlucky, the house total is larger than yours so you lose")
elif playerCount==houseCount:
print("You have then same total as the house so you get your money back")
else:
print("Your total is larger than the house's so you win")