Loops are a big deal in computing and robotics! Think about the kinds of tasks that computers and robots often get used for:
The most basic conditional loop is one that goes on forever. Think about a task where you do the same sequence of operations endlessly - for example production line work often involves seemingly endless loops:
Factories often use robots to do the kind of repetitive work pictured above - for around £20,000 a reconditioned robotic system could pack these crates.
In Python we can use a while loop to keep doing the same thing over and over:
while True:
pick_up_product()
check_product()
put_product_in_crate()
The part in brackets () is called the condition. In this case, we have set the condition to be True permananently. This means the loop would go on forever, which is sometimes what you want. Later we will see how if the condition in the brackets () ever stops being True then the loop would stop.
You are now going to run these two small programs. They should be ready to run, you don't need to edit them.
Run the first program. Once running, you should see that it repeatedly runs its code block until you supply the magic word. Get it wrong first, then enter the magic word
In [ ]:
word = input("What is the magic word? ")
while word!="abracadabra":
word = input("Wrong. Try again. What is the magic word? ")
print("Correct")
If you've done it right, your output should look like this:
What is the magic word? Foobar
Wrong. Try again. What is the magic word? abracadabra
Correct
In Python != means is not equal to
Run the second program. This one will keep repeating its code block until you type in one of the words in the list ["yes", "no"].
In [ ]:
happy = input("Are you happy? ")
while not(happy in ["yes","no"]):
print("I did not understand. ")
happy = input("Are you happy? ")
If you've done it right, your output should look like this:
Are you happy? Naw.
I did not understand.
Are you happy? aye
I did not understand.
Are you happy? yes
By adding some more words to the list ["yes", "no] edit the program below to accept "Yes", "No" and test it by running it a few times
In [ ]:
happy = input("Are you happy? ")
while not(happy in ["yes","no"]): #Add to this list of words here
print("I did not understand. ")
happy = input("Are you happy? ")
Sadly, some infinite loops are unwanted and unloved. You know this situation already: it happens when you get the spinning circle on your phone or computer, and after a while you realise it's never going to finish doing whatever it was supposed to be doing.
(The python while True:
loop at the beginning of the this page can sometimes be useful.)
These three examples will never end. The end user will find that your program has crashed or '...is Not Responding'. Don't try to run these, but do try to see why they will not work.
Fawlty Loop | Why It's Borked |
---|---|
happy = input("Are you happy?") while not(happy in ["yes","no"]): print("I did not understand.") |
The program keeps displaying the error message as the user never gets to type in a new answer (unless you answer correctly on the first attempt). |
number = 1 while number>0: number = number + 1 print(number) |
The program keeps counting forever since number is always bigger than 0. |
gender = input("Enter M or F: ") while gender!="M" or gender!="F": print("Invalid input") gender= input("Enter M or F:") |
If the users types “X”, then this is not equal to “M”, so it is invalid. If the user types “F”, then it is not equal to “M”, so it is invalid. If the user types “M”, then it is not equal to “F”, so it is invalid. Everything is invalid – the “or” in the while should be an “and”. The entire premise of this example ignores non-binary gender too |
You can now complete tasks 32-35 (we will come back to the questions just before Q32 shortly)