while loop unicorn shield en
Over and over again
We have a small problem. Every Python program runs from the top (first line of code) to the bottom (the last line of code). That is awesome because it increases the predictability of our code. Because of this, we know that we can consistently import the libraries in the first line of code and be certain that it will always be the first thing our program will do. Using our blinking LED program as an example, the downside to this is that it also means that it would require alot of work if we wanted to have our LED continue blinking forever. To do this we would need to write a lot of redundant code in a manner similar to below:
unicorn.leftEyeOn() time.sleep(2) unicorn.leftEyeOff() time.sleep(2)
Luckily someone has already created a solution for this problem. We present to you:
The while-loop is somewhat similar to the if-statement because they are both based on a condition. The difference is, as long as the condition is true, the while-loop will continue. If you can remember from before, the if-statement will only check the condition and execute the code once. Let’s have a look at how they appear in real life:
sun = "shining" while sun == "shining": print("Let’s go to the beach")
Looks quite similar, right? There is one thing, however, that you should always keep in mind when working with a while-loop. Because the loop always repeats itself, it may be necessary for you to build a “breakout” point into the code. Without this the loop could keep running until the program is closed externally or until the Pi stops working. In this example we needed to make sure that there is a sunset before ending the loop. Please keep the colon
: at the end of the condition in min. This is important for Python so that it knows when the condition stops and when the intended code to be executed/looped starts.
import unicornshield as unicorn import time blink = "yes" while blink == "yes" unicorn.leftEyeOn() unicorn.rightEyeOn() time.sleep(2) unicorn.leftEyeOff() unicorn.rightEyeOff() blinken = input("Again? yes or no? ")
This next program is really really cool. Lines 1 to 4 are probably already familiar to you, but line 4 is still really important and interesting for us. If we didn’t declare the variable and fill it with the string value "yes", the program would end in line 6. In line 6 the while loop is checking the content of the variable. Because the Variable contains “yes”, the while loop condition evaluates to true and the intended code from line 7 to 12 will be executed. In line 12 we give the user of our program the chance to make their own choice. To do this we use the
input() function. After answering this question the program will go back to line 6 and the while loop will again check the variable “blink”. Depending on the user’s choice either everything from line 7 to 12 will be executed again, or the program will check if there is something to execute after the loop. If this is not the case the program will end.