Python 当余额等于下注时,程序不执行elif语句
每当我运行程序,并且下注值等于余额时,它不会显示我设置为显示的else语句,相反,它会进入程序并输出稍后应该输出的其他信息 下面是一个例子,说明了当赌注不等于余额时,以及当它们相等时会发生什么:Python 当余额等于下注时,程序不执行elif语句,python,Python,每当我运行程序,并且下注值等于余额时,它不会显示我设置为显示的else语句,相反,它会进入程序并输出稍后应该输出的其他信息 下面是一个例子,说明了当赌注不等于余额时,以及当它们相等时会发生什么: 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_a
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
就像人们在评论中说的那样。你的问题是
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)?
所以当你下注等于平衡时,它减去它,条件就不再有效了。您想要的是在while循环中移动减法运算
我注意到的另一个问题是有两个whiles循环,第二个循环总是运行一次。所以甚至没有理由拥有它,用if语句替换它并删除while循环中if语句的break是有意义的
编辑:
在评论中回答作者的问题。如果:
你从余额中减去赌注。之后,while循环的条件可能不再满足。例如。如果你有50,下注50,则余额变为0,并且小于下注。计分根本不应该在一个while循环中,因为你每次只想做一次。你不应该从余额中减去下注,直到玩家掷出一个不匹配的数字。无关,但这将允许玩家以负资金进行负赌注。Balance>0应该是循环不变的。如果我删除第二个If语句,那么当用户被询问是否要再次播放时,它将如何循环?谢谢你的帮助。
Balance -= Bet
...
while Balance >= Bet:
# do rest
...
while Balance >= Bet:
Balance -= Bet
# do rest
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}.")