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

Why does the while loop never stop looping?

Below is the snippet of my code. Let’s assume that the output of play_again() inside the while loop returns False. Then, why does my while loop keep on looping? Is there is some concept I’m unaware of?

game_list = ['0','1','2']

while True:

    position = myfunc()
    
    replacement(game_list,position)
    
    play_again()

print(game_list)

>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

this is b/c the while True does not end unless you use the keyword break wich breaks outside the loop and continues the code.
the while True never ends

the while loop

while (condition):
    #code

never ends until the condition is False, witch would never be true for the True condition.

do your code should be:

game_list = ['0','1','2']

while True:

    position = myfunc()

    replacement(game_list,position)

    if not play_again():
         break
print(game_list)

or you could do:

game_list = ['0','1','2']

while play_again():

    position = myfunc()

    replacement(game_list,position)

print(game_list)
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