如果满足条件,如何进行循环

2024-10-02 18:15:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在做一个赌博游戏,你必须猜一个数字来赚取你下注的金额,但是我希望游戏永远持续下去,只要“玩家”仍然有钱,我怎么做

这是我到目前为止的代码

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
   

Tags: toansweryouinputifdointprint
2条回答

试试这个

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中的一个命令。我建议您研究一下这个函数,以便能够适应您的代码

相关问题 更多 >