如何在满足条件时进行循环



我一直在制作一款赌博游戏,你必须猜一个数字才能赚到你下注的钱,但我希望游戏永远持续下去,只要"玩家";还有钱我该怎么做?

这是我到目前为止写的代码

import random
print("Welcome to gambling simulator the objective is to get as much money as possible without losing it all you start with 500 dollars, GOOD LUCK!")
rngone = int(input("Where do you want your range to be? >>> "))
tries = int(input("How many tries do you want >>> "))
money = 500
bet = int(input("How much do you want to bet >>>"))
winnings = bet*rngone/tries-bet
winnings = int(winnings)
if bet > money:
print("You can't bet more money than you have")


for i in range(tries):
answer = random.randint(1, rngone)
guess = int(input("Guess! >>> "))

if guess == answer:
money = money + winnings
print("CONGRADULATIONS! You won!", winnings ,"You now have", money,"dollars.")
elif guess != answer:
money = money - bet
print("Not quite!", answer,"was the answer. You lost",bet, "you now have",money)
if money == 0:
exit()

elif i == tries:
print("You ran out of tries.")
break

试试这个

import random
print("Welcome to gambling simulator the objective is to get as much money as possible without losing it all you start with 500 dollars, GOOD LUCK!")
rngone = int(input("Where do you want your range to be? >>> "))
tries = int(input("How many tries do you want >>> "))
money = 500
bet = int(input("How much do you want to bet >>>"))
winnings = bet * rngone / tries - bet
winnings = int(winnings)
if bet > money:
print("You can't bet more money than you have")
while money > 0:
if tries <= 0:
query = input('You ran out of tries, do you want to continue? (Y/N) ')
if query.upper() == 'N':
print('Thanks for playing gambling simulator goodbye!')
break
tries = int(input('How many tries do you want? '))
answer = random.randint(1, rngone)
guess = int(input("Guess! >>> "))
if guess == answer:
money = money + winnings
print("CONGRADULATIONS! You won!", winnings, "You now have", money, "dollars.")
elif guess != answer:
money = money - bet
print("Not quite!", answer, "was the answer. You lost", bet, "you now have", money)
tries -= 1

创建一个函数来检查用户余额中是否还有钱

如果为true,则创建一个"for循环"这是python中的一个命令,我建议你研究一下这个函数,这样你就可以适应你的代码

最新更新