Python 2.13: Rescue buoy: while loop
The while loop's lifeline, here we will go back to our two examples concerning the password and the age. when we look at our program we can ask ourselves the question when this question comes to mind you must remember that the execution of a program is done sequentially so it is first line 3 on line 4 after line 5 when the loop and check that it is true it comes out it can execute line 5 line 7 as much for me. It is like that that the program is carried out, it is necessary to put in head that the program its studies of my niece sequential each step in its place to better explain this I will comment this and launch my program you see I am told that on line 4 there is an error because the password is not verified and we could do otherwise I'll put this on top and you'll see the difference you'll understand why we have just declared the password as a string quickly I run my program it asks me for my password if I have entered anything it asks me for the password each time, every time every time repeatedly and when I go to a password that is equal it comes out and it tells me correct password you have access to your account when looking at this program we realize that there is repetition at this level, That's why I decide to delete this and uncomment this to have a well structured program without repetition coherent so the idea of the declaration is to force us to enter a number because if the user hasn't declared before he can't enter this much you're not going to listen to the program the password declared at the top allows the user to execute his while loop in order to compare Well here I will try to enter a password is equal to the initial password that is mimi so I initialize password to mimi and I run my program to see that it will come out of the loop and it will work directly it's this here why because when it has a thing it has the program and that it wants to check and check your password equal to Mimi it's true condition you have we say the loop directly and it shows me this in this program so you have to be very careful when declaring a variable especially the string type always be careful it must never be equal to the initial condition otherwise it will never execute the program. well I'm going to comment this and we will resume our program with the age concerning the age the age str if actually means that we enter a character string and we could rename it otherwise, you said we could take age string and rename it here every time string and that will give perfectly this program could be written in another way, i.e. I could copy this line to here and I make a copy I come back and paste it here like this and this will work perfectly but I run my program you see it asks me the name I put titi the age well I'm going to enter anything already it tells me that there is an error at this level because the age entered normally must be an int and not a string so if I run my program again and I put Titi I enter the age 23 it executes my code perfectly so that means as the program is done in a sequential way after the execution of this it enters the loop it checks if it is correct it continues until this level it displays this but when we look at this program we realize that there is a repetition and the best thing is to put this inside the while loop I'm going to delete this and this too and we could do otherwise by putting at this level an else colon tabs on both lines if I run my program You will see that it's the same thing, everything works normally Oh yeah I forgot to say to declare age equal to 0 I run my program it runs normally because it will enter the loop check if we can convert yes it's true we can convert the string by the user into an int so a type int and continue to execute the program well stop doing like if it's going to enter anything for example here I always put titi and then I have a little bit of anything to see if it executes I put enter That's what normally happens if you have when it's reconverted, so it's wrong, it goes straight into the exception and that's what made me this valley, it wouldn't have asked for the age every time. Each time I enter an invalid age it will ask me again for the age so on until I enter a valid age 24 and it comes out well we say to each other soon the importance of debugging.