Before you start this course, you must login to Replit
If you have forgotten your Replit account user name or password, ask your Coder Coach.
If you have forgotten your Replit account user name or password, ask your Coder Coach.
Iteration is used to loop certain lines of code to prevent the coder having to type them again and again.
There are several different ways of doing this. We are using a conditional iteration called a while loop. This type of loop runs while a condition (or conditions) is/are true. A while loop starts with the command while followed by the condition(s) for the loop to run. The loop will only run while the condition is true, otherwise the program will skip the loop and carry on with the rest of the program. Instructions to be repeated go inside the loop, in python we do this by indenting the code.
In these tasks, you will be given one or more examples of code.
Be able to read, comprehend, trace, adapt and create Python code using selection that:
Previously we have used selection to create error messages for incorrect input, using iteration allows us to get the program to repeat until we get the input that we want.
The plain English algorithm for this looks like:
The condition should be set to be true when the input is NOT what is acceptable.
For text input use != not equal to.
For number range use the ‘outside a range’ conditions from the selection lessons.
Students often miss the ‘get input again inside the loop’ step. If they do this, they will create an infinite loop that never ends because the variable storing the input will never be changed.