[Python] My Baccarat program is not saving the total balance everytime I run my code, what am...

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm very new at programming in python, and I'm trying to make a program to mimic a baccarat game for a school project. Every time I enter "Y" and play again, it resets my total balance. Other than this, everything else seems to be working perfectly fine. I have no idea why it won't save the total balance every time I play again, how can I fix this?

    import time
    import random

    def val(card):
    if card[0] in ['10', 'Jack', 'Queen', 'King']:
    return 0
    elif card[0] == 'Ace':
    return 1
    else:
    return int(card[0])

    def baccarat(bet, total, opt):
    cad = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
    lis = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
    deck = [(card, category) for category in cad for card in lis]
    random.shuffle(deck)
    pCard = [deck.pop(), deck.pop()]
    bCard = [deck.pop(), deck.pop()]
    pSco = sum(val(card) for card in pCard)
    bSco = sum(val(card) for card in bCard)
    print(f"Cards player has: {pCard}")
    pSco = pSco % 10
    print(f"Score of player: {pSco} \n")
    print(f"Cards dealer has: {bCard}")
    bSco = bSco % 10
    print(f"Score of dealer: {bSco} \n")
    if pSco <= 5:
    play = input("Draw another card(P to request another card): ")
    nCard = deck.pop()
    pCard.append(nCard)
    pSco = sum(val(card) for card in pCard)
    print(f"Cards player has: {pCard}")
    pSco = pSco % 10
    print(f"Score of player: {pSco} \n")
    if bSco <= 2:
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    elif bSco == 3:
    nCard = deck.pop()
    while True:
    if val(nCard) < 8:
    break
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    elif bSco == 4:
    nCard = deck.pop()
    while True:
    if val(nCard) >= 2 and val(nCard) <= 7:
    break
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    elif bSco == 5:
    nCard = deck.pop()
    while True:
    if val(nCard) >= 4 and val(nCard) <= 7:
    break
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    elif bSco == 6:
    while True:
    if val(nCard) == 6 or val(nCard) == 7:
    break
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    elif bSco <= 5:
    nCard = deck.pop()
    bCard.append(nCard)
    bSco = sum(val(card) for card in bCard)
    bSco = bSco % 10
    if pSco == bSco:
    if opt == "P" or "B":
    total = total - bet
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Its a tie. \n")
    print(f"You lost {bet} dollars.")
    elif opt == "T":
    total = total + 8*bet
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Its a tie. \n")
    print(f"You won {bet*8} dollars.")
    print(f"Your bank balance is: {total} \n")
    elif pSco > bSco:
    if opt == "P":
    total = total + 2*bet
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Player Wins \n")
    print(f"You won {bet*2} dollars.")
    elif opt == "B" or "T":
    total = total - bet
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Player Wins \n")
    print(f"You lost {bet} dollars.")
    print(f"Your bank balance is: {total} \n")
    else:
    if opt == "B":
    total = total + int(1.9 * bet)
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Player Wins \n")
    print(f"You won {bet*1.9} dollars.")
    elif opt == "P" or "T":
    total = total - bet
    print(f"Cards dealer has: {bCard}")
    print(f"Score of dealer: {bSco} \n")
    print(f"Cards player has: {pCard}")
    print(f"Score of player: {pSco} \n")
    print("Player Wins \n")
    print(f"You lost {bet} dollars.")
    print(f"Your bank balance is: {total} \n")
    def play():
    global betAm
    betAm = int(input("How much would you like to bet?(Minimum of 5): "))
    while True:
    if betAm < 5 or betAm > bal:
    print("Invalid amount. Please try again.")
    betAm = int(input("How much would you like to bet?(Minimum of 5): "))
    else:
    break
    global betOpt
    betOpt = input("Would you like to bet on Player(P), Banker(B) or Tie(T)?: ")
    while True:
    if betOpt != "P" and betOpt != "B" and betOpt != "T":
    print("Invalid input. Please try again.")
    betOpt = input("Would you like to bet on Player(P), Banker(B) or Tie(T)?: ")
    else:
    break

    bal = 500
    rules = input("Would you like to know the rules of baccarat?(Y/N): ")
    while rules != "Y" and rules != "N":
    print("Invalid input. Please try again.")
    rules = input("Would you like to know the rules of baccarat?(Y/N): ")
    if rules == "N":
    time.sleep(1)
    print("Ok. Starting game. \n")
    else:
    if rules == "Y":
    print("In the game, the player bet on whether your hand, the banker's hand, or a tie will win, with the goal of getting a hand closest to 9 in value. \n")
    time.sleep(3)
    print("Ok. Starting game. \n")
    print("You start with $500.")
    play()
    baccarat(betAm, bal, betOpt)
    while bal >= 5:
    again = input("Play again?(Y/N): ")
    while again != "Y" and again != "N":
    print("Invalid input. Please try again.")
    again = input("Play again?(Y/N): ")
    while again == "Y":
    play()
    baccarat(betAm, bal, betOpt)
    again = input("Play again?(Y/N): ")
    else:
    print("Goodbye")
    break
    else:
    print("You are broke. Game over.")

    Continue reading...

Compartilhe esta Página