While/else and for/else


I am getting very confused as to what for/else and while/else are and where they are used. I would really appreciate if someone could clear it up.Thanks guys!


both for/else and while/else, the else will run if the loop condition becomes False

we can also break a loop using the break keyword, then the else won't run

this is very useful as you will see in a couple of exercise (is_prime)

have you already made the guess game? Lets say we want give the user 3 guesses to guess a number between 1 and 10, then we can use while/else to check the number of guesses (while condition) and else print the user lost

in the loop, we use if to check if the user guessed the correct number, if so, break the loop so the loose message won't be printed


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.