In this case, there isn't any more code so your program will stop. Because the condition now evaluates to False, you will exit the while loop and continue your program if it contains any more code. You print out "Thank you" two more times before the value of number is equal to 5 and the condition doesn't evaluate to True any more. While the value in number stays smaller than 5, you continue to execute the two lines of code that are contained within the while loop: "Thank you" Since the value in number is smaller than 5, you print out "Thank you" and increase the value of number with one. If you go into detail in the above code, you see that there is a variable number in which you store an integer 2. The code example above is a very simple while loop: if you think about it, the three components about which you read before are all present: the while keyword, followed by a condition that translates to either True or False ( number < 5) and a block of code that you want to execute repeatedly: print("Thank you") # Increment the value of the variable "number by 1" Now that you know what you need to construct a while loop, all that is left to do now is to look at a real-life example where the while loop is used before you start making exercises on your own! Consider the following example: # Take user input That's all it takes! How To Make A While Loop in Python
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |