In the above piece of code, I have 5 different variables, all declared globally. But as you can see, continuePlaying (wrong naming convention, I know) is greyed out because it can't be accessed. But what I don't understand is that the other variables can be accessed. Why is that? And do I need to put a "global variable_name" line everytime before I use a global variable? Like in the "scores" function below.
Below is the full code if needed:
import random
from art import logo
cards = [11,2,3,4,5,6,7,8,9,10,10,10,10] player_cards = []
casino_cards = [] player_score = 0 casino_score = 0
def initial_draw():
player_cards.append(cards[random.randint(0,12)])
player_cards.append(cards[random.randint(0,12)])
casino_cards.append(cards[random.randint(0,12)])
casino_cards.append(cards[random.randint(0,12)])
print(f"Your cards are:\n{player_cards}")
print(f"Casino's cards are:\n[{casino_cards[0]}, ]")
check_blackjack()
def player_draw():
player_cards.append(cards[random.randint(0,12)])
print(f"Your cards are:\n{player_cards}")
print(f"Casino's cards are:\n[{casino_cards[0]}, ]")
check_score_player()
def casino_draw():
casino_cards.append(cards[random.randint(0,12)])
print(f"Your cards are:\n{player_cards}")
print(f"Casino's cards are:\n{casino_cards}")
def casino_turn():
while sum(casino_cards) < 17:
casino_draw()
if sum(casino_cards) > 21:
game_end("player")
def scores():
global player_score
player_score = sum(player_cards)
global casino_score
casino_score = sum(casino_cards)
def check_blackjack():
if sum(player_cards) == 21 and sum(casino_cards) == 21:
game_end("draw")
elif sum(player_cards) == 21:
game_end("player")
elif sum(casino_cards) == 21:
game_end("casino")
def check_score_player():
if sum(player_cards) > 21:
game_end("casino")
def final_check():
scores()
if (21 - player_score) < (21 - casino_score):
game_end("player")
elif (21 - casino_score) < (21 - player_score):
game_end("casino")
elif (21 - casino_score) == (21 - player_score):
game_end("draw")
def game_end(winner):
scores()
if winner == "player":
print(f"Player won! Player's score: {player_score}\nCasino's score: {casino_score}")
print(f"Your cards were:\n{player_cards}")
print(f"Casino's cards were:\n{casino_cards}")
elif winner == "casino":
print(f"Player lost! Player's score: {player_score}\nCasino's score: {casino_score}")
print(f"Your cards were:\n{player_cards}")
print(f"Casino's cards were:\n{casino_cards}")
elif winner == "draw":
print(f"It was a draw! Player's score: {player_score}\nCasino's score: {casino_score}")
print(f"Your cards were:\n{player_cards}")
print(f"Casino's cards were:\n{casino_cards}")
if input("Do you want to play again? \"y\" or \"n\": ").lower()== "y":
continuePlaying = True
else:
continuePlaying = False
player_cards = []
casino_cards = []
player_score = 0
casino_score = 0
print(logo)
continuePlaying = True
while continuePlaying:
initial_draw()
if input("Do you want to draw another card? \"y\" or \"n\": ").lower()== "y":
wants_to_draw = True
else:
wants_to_draw = False
while wants_to_draw:
player_draw()
if input("Do you want to draw another card? \"y\" or \"n\": ").lower()== "n":
wants_to_draw = False
print(f"Casino's cards are:\n{casino_cards}")
casino_turn()
final_check()