0

enter image description here

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()
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    As a programmer you should try very, *very*, **very** hard not to use globals. – quamrana Aug 05 '22 at 20:16
  • You need `global continuePlaying`. – Barmar Aug 05 '22 at 20:16
  • 1
    You need `global` for variables that you assign. You don't need it for variables that you only read. – Barmar Aug 05 '22 at 20:17
  • 2
    Yes, if you want to assign to a global variable inside a function you need to have `global continuePlaying` near the start of that function. (Just like in the `scores()` function) – quamrana Aug 05 '22 at 20:17
  • 1
    I assume that variable is greyed out because it's useless. It is a _local_ variable inside the `game_end()` function that is _assigned to_, but never actually _used_. – John Gordon Aug 05 '22 at 20:18

0 Answers0