Home > database >  How to make a loop if a condition is met
How to make a loop if a condition is met

Time:10-07

I have been working on making a gambling game where you must guess a number to earn the amount of money that you bet however I want the game to continue forever as long as the "Player" still has money how would I do this?

Here is the code I have so far

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
   

CodePudding user response:

Try this

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

CodePudding user response:

create a function to check if the user still has money in the balance

if true then create a "for loop" it is a command in python I suggest you do research on this function so you can adapt into your code

  • Related