{SOLVED} while loop wont stop running in python

I am working on a crash game in python and im trying to make it crash when a value hits 1,50, or 100 but it keeps going. Does anyone know why?

import random
import time

game = 1
num = 1.00

while game == 1:
    num += .01
    print(round(num, 2))
    time.sleep(.1)
    if game != 1:
        print("Crash!")
        break

while game == 1:
    crash = random.randint(1, 100)
    time.sleep(.1)
    if crash == 1 or crash == 50 or crash == 100:
        game -= 1
    else:
        break

>Solution :

In first while loop game’s value never changes from 1 therefore never exits the first while loop

BTW you do not have to break the loop because once game != 1 it will won’t enter the loop again.

I think you mean to use only the second while loop you have written – so you can delete the first one and remove the else from the second – no need for it for the same reason you do not need the break in the first loop.

so if I understand your intention correctly than the code should be:

import time

game = 1

while game == 1:
    crash = random.randint(1, 100)
    time.sleep(.1)
    if crash == 1 or crash == 50 or crash == 100:
        game -= 1

Leave a Reply