Programs such as those that operate our air conditioners must repeat until they are stopped by a person. This is also true for the programs of our robots. Here's how to make your program repeat until it meets a condition set by you.
- 11 Jun 2020
The "while" repeats the code set by him while the condition set by you is true/met.
Here's what this operator would look like in your code:
while condition: (code that repeats when the condition returns false) (code that will execute after the end of the loop)
⚠️ Notice the colon after the condition. It's common to forget to place it and students wonder why the program doesn't work!
If you set a condition that will always be met, then the code inside the "while" operator will repeat until you manually stop it. An example of such a condition is "True":
while True: (code that will execute indefinitely) (code that will never be reached)
In such a case, the program will indefinitely repeat the code that's within the "while" operator, which is why it will never execute the code after it.
Courses and lessons with this Tutorial
This Tutorial is used in the following courses and lessons