Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to make a loop that doesn't lose the value of a name when it loops in python

+9 votes
asked Apr 23 by trash disposal (250 points)
I have this code:

import random
money = 50
bet = int(input("You have 50 dolars, how much money would you like to bet?"))
if bet > money:
    print("You don't have all that money!")
elif bet <= money:
    money = money - bet
guess = int(input("Choose one number in between 1 and 10"))
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
random_numbers = random.choice(numbers)
print("The number drawn was:", random_nada, "!")
if guess == random_number:
    money = money + bet +bet
    print("Congratulations! You guessed right and doubled you bet! You now have:", ponto, "!")
else:
    print("You guessed wrong and lost the value that you bet. You now have:", ponto, ".")
ans = input("Would like to play again?")
if ans == "yes" or ans == " yes": print("You guessed wrong and lost the value that you bet. You now have:", ponto, ".")
ans = input("Would like to play again?")
if ans == "yes" or ans == " yes"
      here i wanted the code to restart but without losing the current value of "money", or else the player would just have 50 dolars at the start regardless of how much he won or lost beting along the game.

2 Answers

0 votes
answered Apr 25 by trash disposal (250 points)
nvm, i got it
0 votes
answered Apr 25 by ANTHONY JOSUE PALLCHISACA VALVERDE (160 points)

Here i add validator for each entry to avoid the errors in the inputs, next i add a bucle while that works everytime you have money

import random

money = 50

while money>0:

    while True:

        try:

            bet = int(input(f"You have {money} dollars, how much money would you like to bet? "))

            if bet > money:

                print("You don't have all that money!")

            elif bet <= money:

                money = money - bet

                break

        except ValueError:

            print("You don't have all that money!")

    while True:

        try:

            guess = int(input("Choose one number in between 1 and 10: "))

            numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

            if guess in numbers:

                break

        except:

            print("You don't choose a correct number")

    random_numbers = random.choice(numbers)

    print("The number drawn was:", random_numbers, "!")

    if guess == random_numbers:

        money = money + 2*bet

        print("Congratulations! You guessed right and doubled you bet! You now have:", money, "!")

    else:

        print("You guessed wrong and lost the value that you bet. You now have:", money, ".")

        if money == 0:

            break

    while True:

        try:

            ans = input("Would like to play again? yes/no: ")

            if ans == "yes" or ans == " no":

                break

        except:

            print("You don't wire a correct answer")

    if ans == "yes":

        print("Let's try again")

    else:

        print("Goodbye Friend")

        break

print("Sorry friend you WASTED")

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...