Reputation:
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")
Upvotes: 1
Views: 1193
Reputation: 9978
A good design practice to follow when coding in Python is to break up as much as possible into small, self-contained functions. This is what you have started to do with your housePlay
function. Unfortunately for you and your code, Python also has scoping rules. That is, a variable defined within a function is local to that function (can't be seen from outside the function), and variables called from within a function are first looked up within the enclosing function (L), then any enclosing function (E), then the top level of a module file or global (G), then built-in (B). See the top answer here - Short Description of the Scoping Rules?
In your case, you just have a python script, not a module. A module has an associated __init__.py
file in the same directory. Because of this, the variables you have defined at the top level of the file (playerCount
, houseCount
etc) won't be visible from within each function. (It can't be found in L, there is no E, you do not have a module and haven't defined them as global (G). This is the cause of your "variable referenced before assignment" error.
It is good practice to pass any objects (variables) needed by a function to the function as function arguments:
def housePlay(playerCount, houseCount):
....some code here....
You would need to call this function as:
housePlay(playerCount, houseCount)
Alternatively, you can define each top-level variable as global
:
global playerCount = 0
global houseCount = 0
Possibly the better option is to use the if __name__ == '__main__':
construct.This looks like this:
import random
def main():
playerCount = 0
houseCount = 0
...rest of your code...
if __name__ == '__main__':
main()
All your code is contained within the main()
function (although you would usually have other functions defined alongside main
), and if you are running the script directly and not importing it as part of another script (that's what the if __name__ == '__main__'
does) it will just run everything in the main()
function.
Doing things this way will ensure that any inner functions (such as housePlay
) can see the variables defined in your enclosing (E) scope (in this case, the main
function).
Upvotes: 0
Reputation: 629
Add global houseCount
at the beginning of your function housePlay()
Explanation : as it is said in a comment, the variable you are trying to look at, houseCount
, is not found in the function before being assigned a value. You can use the other variables (a
, d
) without troubles, because they are not modified in the function.
As you change houseCount
's value in the function, you must declare it as global, so the function knows the variable is already defined on the global scope.
Upvotes: 1
Reputation: 12599
def housePlay(houseCount):
Pass houseCount as argument in your function defination
housePlay(houseCount)
call housePlay function like this.
Upvotes: 3
Reputation: 1325
For now, just do global houseCount
within the housePlay()
function - that should get you over the hump.
Upvotes: 1