Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

{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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading