While…

--Originally published at PYTHON 3

The whiles are useful when we need to make a loop in our code, by this I mean that after we are done with a set of instructions maybe we want to repeat the same instructions one more time, and another and another and another….., until a statement tells it to stop or even the user wants to make it stop.

The while differentiates from the for because we use when we don’t know how many times the code will execute, with the for we tell the program how many times to repeat the loop.

screenshot-from-2016-09-09-09-10-58

Here I am telling the program to generate a random number from 0 to 10, I am also creating an int variable x =1,

so if my random number is less or equal than 20, my random number will be added the x=1, and after that I am printing all the numbers till it gets equal to 20.

This is a while loop because I don’t know my initial number, maybe my initial random is 0 or 10, and one may need more loops to reach 20 than the another, that’s why I use a while, because I don’t know how many times to repeat the instructions until it reach the goal.

screenshot-from-2016-09-09-09-21-37

screenshot-from-2016-09-09-09-21-53

I learned while loops from here…? Check it out

http://www.python-course.eu/python3_loops.php