Loops
We have used loops since the start of the course. Here we will learn how to build our own.
Last updated
We have used loops since the start of the course. Here we will learn how to build our own.
Last updated
Loops allow us to repeat code a certain number of times or until a specific condition is met
A for loop in Python iterated over or repeats a list of items - like the number 1-100. The following code iterates over 0,1,2,3 (remember that Python starts counting at 0)
The code above makes a shape called a Rosette, made up from 4 circles.
Some examples of ranges:
range(5) = 0, 1, 2, 3, 4
range(1) = 0
range(10) = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
range(2, 5) = 2 , 3 , 4
range(-2, 2) = -2, -1, 0, 1
To experiment with ranges, you can write out the numbers in the Idle Shell using the command list(range(4))
Modify the for loop in the code above to draw a Rosette made from 10 circles. It should like look the picture below.
It would be much better if we could ask the user how many circles they want to draw. We can ask the user for a number using Turtle with the code:
This stores the number in a variables called numberOfCircles.
Modify your code so that the it asks the users how many circles they want and then draw them to the screen.
Using the Turtle commands that you have already learnt, experiment to make more interesting patterns. For example, try adding a second smaller circle straight after the first.
while
loop repeats the sequence of actions many times until some condition evaluates to False
. The condition is given before the loop body and is checked before each execution of the loop body. Typically, the while
loop is used when it is impossible to determine the exact number of loop iterations in advance. This is very common in games where the loop is known as the GAME LOOP.
The syntax of the while
loop in the simplest case looks like this:
Python firstly checks the condition. If it is False, then the loop is terminated and control is passed to the next statement after the while
loop body. If the condition is True, then the loop body is executed, and then the condition is checked again. This continues while the condition is True. Once the condition becomes False, the loop terminates and control is passed to the next statement after the loop.
For example, the following program fragment prints the squares of all integers from 1 to 10.
Ask the user for a number and use a while loop to output the sum of integers 1,2,3.. etc up to that number. Remember that to do maths with an input we have to inclose the input function in eval() to tell Python that it is a number. eg.
number = eval(input('Which number'))
We can also make the condition related to a string. Here we ask the user for a name and repeat it 100 times before asking them again. Only when the user enters an empty string do we exit the loop
Games are better with graphics though. Check out this simple game:
Improve the code by including command to go right and backwards
Modify the code to create a better game. Eg. create a race track before the book starts so that the user has to go around the track in the smallest number of moves
As a future athlete you just started your practice for an upcoming event. Given that on the first day you run x miles, and by the event you must be able to run y miles.
Calculate the number of days required for you to finally reach the required distance for the event, if you increases your distance each day by 10% from the previous day.
Print one integer representing the number of days to reach the required distance.