Python 对金钱问题进行负分

Python Score Issues with Money in Negatives

我正在制作游戏。有一个问题。我当前的代码是这样的:

import random
print("Welcome to Python Acey Ducey Card Game")
print()
print("Acey-ducey is played in the following manner: the dealer (computer) deals two cards faced up and you have an option to bet or not bet depending on whether or not you feel the card will have a value between the first two. If you do not want to bet, enter a [=11=] bet.")
bankbalance = 100
def main():
  global bankbalance
  print()
  print("These cards are open on the table:")
  print()
  print("First card:")
  firstcard = random.randint(1,13)  
  print(firstcard)
  print("Second card:")
  secondcard = random.randint(1,13)
  print(secondcard)
  playerinput = input("Enter your bet: ") 
  playerinput = int(playerinput)  
  dealercard = random.randint(1,13)
  dealercard = int(dealercard)
  print("The card you drew was", (dealercard), "!")
  if dealercard > firstcard and dealercard < secondcard or dealercard < firstcard and dealercard > secondcard:
    print("You win!")
    bankbalance = bankbalance + playerinput
    print("You currently have $", (bankbalance))
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      exit
  elif playerinput > (bankbalance):
    print("You cannot bet more than you have!")
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      exit
  elif bankbalance == (0):
    print("Oh no! You have no more money to bid.")
    exit
  else:
    print("You lost!")
    bankbalance = bankbalance - playerinput
    print("You currently have $", (bankbalance))
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      exit
main()

问题是您的金额可能为负数。我该如何解决?我试过这个:

elif bankbalance == (0):
    print("Oh no! You have no more money to bid.")
    exit

但这似乎也不起作用。有答案吗?我已尝试解决该问题,但我的控制台不执行此操作。它只是说“您目前有 0 美元”。

您需要在每次调用您的函数时检查余额是否等于 0。因此,在继续之前,您需要在 global bankbalance:

下添加 if 语句
import random
print("Welcome to Python Acey Ducey Card Game")
print()
print("Acey-ducey is played in the following manner: the dealer (computer) deals two cards faced up and you have an option to bet or not bet depending on whether or not you feel the card will have a value between the first two. If you do not want to bet, enter a [=10=] bet.")
bankbalance = 100
def main():
  global bankbalance
  if bankbalance == 0:
      print("Oh no! You have no more money to bid.")
      return
  print()
  print("These cards are open on the table:")
  print()
  print("First card:")
  firstcard = random.randint(1,13)  
  print(firstcard)
  print("Second card:")
  secondcard = random.randint(1,13)
  print(secondcard)
  playerinput = input("Enter your bet: ") 
  playerinput = int(playerinput)  
  dealercard = random.randint(1,13)
  dealercard = int(dealercard)
  print("The card you drew was", (dealercard), "!")
  if dealercard > firstcard and dealercard < secondcard or dealercard < firstcard and dealercard > secondcard:
    print("You win!")
    bankbalance = bankbalance + playerinput
    print("You currently have $", (bankbalance))
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      return
  elif playerinput > (bankbalance):
    print("You cannot bet more than you have!")
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      return
  else:
    print("You lost!")
    bankbalance = bankbalance - playerinput
    print("You currently have $", (bankbalance))
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
      main()
    else:
      return
main()

当您的银行余额达到 0 时的结果:

...
First card:
2
Second card:
2
Enter your bet: 100
The card you drew was 3 !
You lost!
You currently have $ 0
Would you like to play again y/n: y
Oh no! You have no more money to bid.
import random
print("\n\tWelcome to Python Acey Ducey Card Game\t\n")
print("Acey-ducey is played in the following manner: the dealer (computer) deals two cards faced up and you have an option to bet or not bet depending on whether or not you feel the card will have a value between the first two. If you do not want to bet, enter a [=10=] bet.")

bankbalance = 100

def main():
    global bankbalance
    print("\nThese cards are open on the table:\n")
    print("First card:")
    firstcard = random.randint(1,13)  
    print(firstcard)
    print("Second card:")
    secondcard = random.randint(1,13)
    print(secondcard)
    playerinput = int(input("Enter your bet: "))   
    if playerinput > (bankbalance):
        print("You cannot bet more than you have!")
        play_again()
    dealercard = random.randint(1,13)
    dealercard = int(dealercard)
    print(f"The card you drew was {dealercard} !")
    
    if dealercard > firstcard and dealercard < secondcard or dealercard < firstcard and dealercard > secondcard:
        print("You win!")
        bankbalance += playerinput
        print(f"You currently have $ {bankbalance}")
        play_again()
    elif bankbalance <= 0:
        print("Oh no! You have no more money to bid.")
        exit()
    else:
        print("You lost!")
        bankbalance -= playerinput
        print(f"You currently have $ {bankbalance}")
        play_again()

def play_again():
    playagain = input("Would you like to play again y/n: ")
    if playagain == ("y"):
        main()
    else:
        exit()
main()

我稍微重写了你的代码。

import random
bank_balance = 100
play_again = True


def random_card() -> int:
    return random.randint(1,13)

def set_bet():
    try:
        bet = int(input("Enter your bet: "))
    except ValueError:
        bet = 0
    return bet

def play():
    global bank_balance
    print("These cards are open on the table:")
    first_card = random_card()
    second_card = random_card()
    print(f"First card: {first_card}")
    print(f"Second card: {second_card}")
    bet = set_bet()

    while (bet < 0) or (bet > bank_balance):
        if bet > bank_balance:
            print("You cannot bet more than you have!")
        elif bet < 0:
            print("You cannot bet <= 0 ")
        bet = set_bet()
    
    player_card = random_card()
    print(f"The card you drew was {player_card}!")
    if sorted([first_card, second_card, player_card])[1] == player_card:
        bank_balance += bet
        print('you win!')
    else:
        bank_balance -= bet
        print('you lost!')

def main():
    print("Welcome to Python Acey Ducey Card Game")
    print()
    print("Acey-ducey is played in the following manner: the dealer (computer)"
          " deals two cards faced up and you have an option to bet or not bet"
          " depending on whether or not you feel the card will have a value"
          " between the first two. If you do not want to bet, enter a [=10=] bet.")

    global play_again, bank_balance
    while play_again:
        print(f"You currently have $ {bank_balance}")
        play()
        if bank_balance <= 0:
            print("Oh no! You have no more money to bid.")
            play_again = False
        else:
            play_again = input("Would you like to play again y/n: ") == 'y'

main()