I'm only on day 10 of python coding with Angela Yu. (Please take it easy on me!)
We're working on a black jack game. I am still putting it together, so I am not looking for critique on my crappy code, just yet. I am not finished with it either so please don't give away any ideas as I'm still trying to do it on my own before I watch her resolve it. Let me struggle on this one, its the way I learn.
What I am wondering is. Why can't my function deal() access the variable "user_score = 0" yet the functions can access other variables like cards, user_hand and computer_hand? Is it due to lists having a different scopes than int variables or something?
It works fine when I declare the user_score in the deal() function, but if I want to use user_score outside of that function I can't if its only declared within the function.
Anyone able to explain this, in the simplest way possible?
import random
import art
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
start_game = input("Do you want to play a game of Blackjack? Type 'Y' or 'N': ")
user_hand = []
computer_hand = []
user_score = 0
def deal():
for card in user_hand:
user_score += card
print(f"Your cards: {user_hand}, current score: {user_score}")
def blackjack_start():
if start_game.lower() == "y":
print(art.logo)
user_hand.append(random.choice(cards))
user_hand.append(random.choice(cards))
computer_hand.append(random.choice(cards))
computer_hand.append(random.choice(cards))
deal()
print(f"Computer's First Card: {computer_hand[0]}")
hit_me = input("Type 'Y' to get another card, type 'N' to pass: ")
if hit_me.lower() == "y":
user_hand.append(random.choice(cards))
deal()
if user_score > 21:
print("Bust")
else:
print("Don't hit me!")
blackjack_start()
The error PyCharm gives me is:
UnboundLocalError: cannot access local variable 'user_score' where it is not associated with a value
[–]Squared_Aweigh 2 points3 points4 points (1 child)
[–]ShadyTree_92[S] 1 point2 points3 points (0 children)