[Solved] Infinite Loop Error in Python


The code you posted won’t do anything but pick 3 psuedo-random numbers until the end of time. You have to add some win/loss conditions. As of now the x, y and z are just numbers. If you want to make a gambling game you have to add some victory conditions like:

if x + y + z > 10

just an example but your program needs to be able to tell if the player won. then it needs to make changes to the players total amount of money and ask for a new wager. You also might want to add logic to make sure the player can’t bet more than they have.

import random
coins = 1000
wager = 0
while True: #main loop
    print('you have {} coins'.format(coins))
    if coins == 0: #stops the game if the player is out of money
        print('You are out of money! Scram, deadbeat!')
        break
    while wager > coins or wager == 0: #loops until player enters a non-zero wager that is less then the total amount of coins
        wager = int(input('Please enter your bet (enter -1 to exit): '))
    if wager < 0: # exits the game if the player enters a negative
        break
    print('All bets are in!') 
    x = random.randint(0,10)
    y = random.randint(0,10)
    z = random.randint(0,10)
    print(x,y,z) #displays all the random ints
    if x + y +z > 10: #victory condition, adds coins for win
        print('You win! You won {} coins.'.format(wager))
        coins += wager
    else: #loss and deduct coins
        print('You lost! You lose {} coins'.format(wager))
        coins -= wager
    wager = 0 # sets wager back to 0 so our while loop for the wager validation will work

solved Infinite Loop Error in Python