首页 > 解决方案 > 当 balance 等于 bet 时程序不执行 elif 语句

问题描述

import random

print("Welcome to the Dice Game!")
Balance = int(input("How much money would you like to play with?:"))

Dice_again = "yes"

while Dice_again.casefold() == "y" or Dice_again.casefold() == "yes":
    Bet = int(input("How much would you like to bet?:"))
    if Bet > Balance:
        print("Sorry, you dont not have enough funds to bet this much.")
        print(f"You have ${Balance}.")
        continue
    elif Balance == 0:
        print("Sorry, you have $0 and cannot play the game anymore.")
        break
    Betnumber = int(input("What number would you like to bet on?:"))
    if Betnumber > 12:
        print("You have entered an invalid input.")
        continue
    Dice1 = random.randint(1,6)
    Dice2 = random.randint(1,6)
    Balance -= Bet
    Numberrolled = (Dice1 + Dice2)
    print("Now rolling....")
    while Balance >= Bet:
        if (Betnumber == 2) and (Numberrolled == 2):
            Winning1 = Bet * 36
            print(f"Congratulations! You rolled a 2 and won ${Winning1}!")
            Balance += Winning1
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 3) and (Numberrolled == 3):
            Winning2 = Bet * 18
            print(f"Congratulations! You rolled a 3 and won ${Winning2}!")
            Balance += Winning2
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 4) and (Numberrolled == 4):
            Winning3 = Bet * 12
            print(f"Congratulations! You rolled a 4 and won ${Winning3}!")
            Balance += Winning3
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 5) and (Numberrolled == 5):
            Winning4 = Bet * 9
            print(f"Congratulations! You rolled a 2 and won ${Winning4}!")
            Balance += Winning4
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 6) and (Numberrolled == 6):
            Winning5 = Bet * 7
            print(f"Congratulations! You rolled a 2 and won ${Winning5}!")
            Balance += Winning5
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 7) and (Numberrolled == 7):
            Winning6 = Bet * 6
            print(f"Congratulations! You rolled a 2 and won ${Winning6}!")
            Balance += Winning6
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 8) and (Numberrolled == 8):
            Winning7 = Bet * 7
            print(f"Congratulations! You rolled a 2 and won ${Winning7}!")
            Balance += Winning7
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 9) and (Numberrolled == 9):
            Winning8 = Bet * 9
            print(f"Congratulations! You rolled a 2 and won ${Winning8}!")
            Balance += Winning8
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 10) and (Numberrolled == 10):
            Winning9 = Bet * 12
            print(f"Congratulations! You rolled a 2 and won ${Winning9}!")
            Balance += Winning9
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 11) and (Numberrolled == 11):
            Winning10 = Bet * 18
            print(f"Congratulations! You rolled a 2 and won ${Winning10}!")
            Balance += Winning10
            print(f"You now have ${Balance}.")
            break
        elif (Betnumber == 12) and (Numberrolled == 12):
            Winning11 = Bet * 36
            print(f"Congratulations! You rolled a 2 and won ${Winning11}!")
            Balance += Winning11
            print(f"You now have ${Balance}.")
            break
        else:
            print(f"Sorry, but you rolled a {Numberrolled}.")
            print(f"You now have ${Balance}.")
            break

    Dice_again = input("Would you like to play again (N/Y)?")
    if Dice_again.casefold() == "yes" or Dice_again.casefold() == "y":
        continue
    elif Dice_again.casefold() == "no" or Dice_again.casefold() == "n":
        print(f"You have stopped the game with ${Balance} in your hand.")
        break
    else:
        print("You have entered an invalid input.")
        break

每当我运行程序并且下注值等于余额时,它不会显示我设置为显示的 else 语句,而是会关闭程序并输出应该稍后输出的其他信息。

以下是下注不等于余额以及彼此相等时发生的情况的示例:

Welcome to the Dice Game!
How much money would you like to play with?:100
How much would you like to bet?:50
What number would you like to bet on?:12
Now rolling....
Sorry, but you rolled a 7.
You now have $50.
Would you like to play again (N/Y)?yes
How much would you like to bet?:50
What number would you like to bet on?:12
Now rolling....
Would you like to play again (N/Y)?

标签: python

解决方案


就像人们在评论中所说的那样。你遇到的问题是这个

Balance -= Bet
...
while Balance >= Bet:
    # do rest

因此,当您Bet等于时,Balance它将减去它并且条件不再有效。你想要的是在while循环内移动减法

...
while Balance >= Bet:
    Balance -= Bet
    # do rest

我注意到的另一个问题是您有两个 while 循环,而第二个循环将始终运行一次。所以甚至没有理由拥有它,用if语句替换它并删除此循环break内部的语句内部是有意义的。ifwhile

编辑:在评论中回答作者的问题。你替换 make second whilean if

if Balance >= Bet:
    Balance -= Bet
    if (Betnumber == 2) and (Numberrolled == 2):
        Winning1 = Bet * 36
        print(f"Congratulations! You rolled a 2 and won ${Winning1}!")
        Balance += Winning1
        print(f"You now have ${Balance}.")
    elif (Betnumber == 3) and (Numberrolled == 3):
        Winning2 = Bet * 18
        print(f"Congratulations! You rolled a 3 and won ${Winning2}!")
        Balance += Winning2
        print(f"You now have ${Balance}.")
    elif (Betnumber == 4) and (Numberrolled == 4):
        Winning3 = Bet * 12
        print(f"Congratulations! You rolled a 4 and won ${Winning3}!")
        Balance += Winning3
        print(f"You now have ${Balance}.")
    elif (Betnumber == 5) and (Numberrolled == 5):
        Winning4 = Bet * 9
        print(f"Congratulations! You rolled a 2 and won ${Winning4}!")
        Balance += Winning4
        print(f"You now have ${Balance}.")
    elif (Betnumber == 6) and (Numberrolled == 6):
        Winning5 = Bet * 7
        print(f"Congratulations! You rolled a 2 and won ${Winning5}!")
        Balance += Winning5
        print(f"You now have ${Balance}.")
    elif (Betnumber == 7) and (Numberrolled == 7):
        Winning6 = Bet * 6
        print(f"Congratulations! You rolled a 2 and won ${Winning6}!")
        Balance += Winning6
        print(f"You now have ${Balance}.")
        break
    elif (Betnumber == 8) and (Numberrolled == 8):
        Winning7 = Bet * 7
        print(f"Congratulations! You rolled a 2 and won ${Winning7}!")
        Balance += Winning7
        print(f"You now have ${Balance}.")
    elif (Betnumber == 9) and (Numberrolled == 9):
        Winning8 = Bet * 9
        print(f"Congratulations! You rolled a 2 and won ${Winning8}!")
        Balance += Winning8
        print(f"You now have ${Balance}.")
        break
    elif (Betnumber == 10) and (Numberrolled == 10):
        Winning9 = Bet * 12
        print(f"Congratulations! You rolled a 2 and won ${Winning9}!")
        Balance += Winning9
        print(f"You now have ${Balance}.")
    elif (Betnumber == 11) and (Numberrolled == 11):
        Winning10 = Bet * 18
        print(f"Congratulations! You rolled a 2 and won ${Winning10}!")
        Balance += Winning10
        print(f"You now have ${Balance}.")
        break
    elif (Betnumber == 12) and (Numberrolled == 12):
        Winning11 = Bet * 36
        print(f"Congratulations! You rolled a 2 and won ${Winning11}!")
        Balance += Winning11
        print(f"You now have ${Balance}.")
    else:
        print(f"Sorry, but you rolled a {Numberrolled}.")
        print(f"You now have ${Balance}.")

推荐阅读