0

im a beginner programmer and im trying to make a simple blackjack game just for exercise in Python 3.10 using PyCharm CE but I wasn't able to solve a problem. I put my whole code but put lots of #'s for important parts(imo).

Problem is , I set my player_score variable to 0 before code runs at ...(1) , but I can't change it later on at ...(2). I feel like computer thinks they are not the same variable because when you select the "player_score" word with your mouse, PyCharm supposed to highlight all of the words with the same name but it doesn't happen. PyCharm see all the "player_score"'s are the same in calculate_player_score() function, but different from the one at ...(1)

import random
import art

print(art.logo)
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_score = 0 ################################# Here i set my variable to 0 ...(1)
computer_score = 0
player_cards = []
computer_cards = []


def player_draw_card():
    card = random.choice(cards)  
    player_cards.append(card)  


def computer_draw_card():
    index = random.randint(0, 12)  
    computer_cards.append(cards[index])  


def calculate_player_score():  
    player_score = sum(player_cards)   ############## Here i try to change my variable. ...(2)
    if player_score > 21 and (11 in player_cards):
        index_of_11 = player_cards.index(11)
        player_cards[index_of_11] = 1
        player_score = sum(player_cards)


def calculate_computer_score():  
    computer_score = sum(computer_cards)
    if computer_score > 21 and (11 in computer_cards):
        index_of_11 = computer_cards.index(11)
        computer_cards[index_of_11] = 1
        computer_score = sum(computer_cards)


def blackjack():
    player_lost = False
    player_score = 0
    computer_score = 0
    player_cards.clear()
    computer_cards.clear()
    player_draw_card()
    player_draw_card()
    print(f"Your cards: {player_cards}")
    calculate_player_score() #################### This is where i call the function and wont work    computer_draw_card()
    print(f"Computer's first card: {computer_cards}")
    calculate_computer_score()
    player_wants_more = True
    while player_wants_more:
        y_or_n = input("Type 'y' to get another card, type 'n' to pass:").lower()
        if y_or_n == "n":
            player_wants_more = False
        if y_or_n == "y":
            player_draw_card()
            calculate_player_score()
            print(player_cards)
            if player_score > 21:
                player_lost = True
                player_wants_more = False
        print(player_score)
    if player_lost:
        print("You lost!\n")
        restart = input("If you want to play again type 'y' if you want to finish the game type 'n'")
        if restart == "y":
            blackjack()
    while computer_score < 17:
        computer_draw_card()
        calculate_computer_score()
    if computer_score < player_score:
        print("You win!\n")
    elif player_score < computer_score:
        print("You lose")
    else:
        print("It's a draw!")
    y_or_n = input("Type 'y' if you want to play another game and 'n' if you dont.").lower()
    if y_or_n == 'y':
        blackjack()
    else:
        print("Bye")


blackjack()


x = 2 x = 3 When I write this line of code, computer thinks there is only 1 variable called "x" right? And it changed from 2 to 3. It's not like there is multiple variable called "x". So what's wrong with my code, why are they not treated as same variable.

Begmattea
  • 11
  • 4
  • you need to read up on "variable scopes" in python – Paul H Nov 04 '22 at 19:33
  • Yeah thank you for commenting, these "keywords" are hard to find if you dont know them. I will fix it by myself after reading and learning about them. – Begmattea Nov 04 '22 at 19:43

0 Answers0