[Python] "Variable" not accessed

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Blackjack program and everything works fine except my PlayAgain() function. In this function, im supposed to reset my variables but when i do so, the variables are discolored in a darker blue and when hovered over, it says "variable" is not accessed. When running the program and trying to play again, the variables are still left with the previous values. I'm pretty new to python so I have no idea how to deal with this, help would be nice.

    [​IMG]

    here is my ENTIRE code:

    import random

    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    dealer = []
    hand = []
    random.shuffle(deck)

    def DealDealer(deck):
    while len(dealer) < 2:
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    dealer.append(card)
    return dealer

    def DealPlayer(deck):
    while len(hand) < 2:
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    hand.append(card)
    return hand

    def PlayAgain():
    again = input("\nDo you want to play again? (Y/N) : ").lower()
    if again == "y":
    Game()
    deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]*4
    dealer = []
    hand = []
    random.shuffle(deck)
    else:
    print("Bye!")
    exit()

    def Hit():
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    hand.append(card)
    return hand

    def DealerHit():
    card = deck.pop()
    if card == 11:card = "J"
    if card == 12:card = "Q"
    if card == 13:card = "K"
    if card == 14:card = "A"
    dealer.append(card)
    return dealer

    def HandTotal(hand):
    total = 0
    for card in hand:
    if(card == "J" or card == "Q" or card == "K"):
    total+= 10
    elif(card == "A"):
    if(total >= 11):
    total+= 1
    else:
    total+= 11
    else:
    total += card
    return total

    def DealerTotal(dealer):
    total = 0
    for card in dealer:
    if(card == "J" or card == "Q" or card == "K"):
    total+= 10
    elif(card == "A"):
    if(total >= 11):
    total+= 1
    else:
    total+= 11
    else:
    total += card
    return total

    def DealersRetribution():
    while(DealerTotal(dealer) < HandTotal(hand)):
    DealerHit()
    print("Dealer's Hand: ", dealer)

    if(DealerTotal(dealer) > 21):
    print("\Dealer Busted! You Win!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    PlayAgain()

    def Score():
    if(HandTotal(hand) == DealerTotal(dealer)):
    print("\nTie! I'll getcha next time!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    elif(HandTotal(hand) > DealerTotal(dealer)):
    print("What?! You Won?! How?! U Just got lucky.\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    else:
    print("\nYou Lose!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))

    def NormalAction():
    action = input("\nHit - press 1\nStand - press 2\nDouble Down - press 3\n> ")
    if(action == "1"):
    Hit()
    print("\nDealer's Hand: ", dealer)
    print("Your Hand: ", hand)
    if(HandTotal(hand) > 21):
    print("\nYou Busted!\nYour Score: ", HandTotal(hand), "\nDealer's Score: ", DealerTotal(dealer))
    PlayAgain()
    else:
    NormalAction()
    elif(action == "2"):
    print()
    DealersRetribution()
    print("\nDealer's Hand: ", dealer)
    print("Your Hand: ", hand)
    Score()
    elif(action == "3"):
    Hit()
    print("\nDealer's Hand: ", dealer)
    print("Your Hand: ", hand)
    DealersRetribution()
    print("\nDealer's Hand: ", dealer)
    print("Your Hand: ", hand)
    Score()
    else:
    print("\nPlease enter a correct action (1/2)!")
    NormalAction()

    def Game():
    play = input("\nWELCOME TO BLACKJACK!\nPlay - press 1\nQuit - press 2\n> ")
    if(play == "2"):
    print("\nBye, Have a nice day!")
    exit()
    elif(play == "1"):
    DealDealer(deck)
    DealPlayer(deck)
    print("\nDealer's Hand: ", dealer[0], "X")
    print("Your Hand: ", hand)
    NormalAction()
    PlayAgain()
    else:
    print("\nPlease enter a correct action (1/2)!")
    Game()

    Game()


    Thx in advance.

    ps: I've been trying to get splitting(blackjack splitting, not programming splitting) to work but have no idea how to go about it. I originally thought that i could just make an array inside an array but quickly realized that caused all sorts of problems. Help on this would also be nice but the Error is the main issue right now

    edit: a request for where i defined my variables were ask. I defined them at the very top of my program. here

    Continue reading...

Compartilhe esta Página