7_3_4

WHAT: Use repetition in programs

HOW:

Activity 1 - READ

You saw repetition in 7_2_4 when you looked at some Scratch code. Repetition is exactly as the word describes, it just means getting our code to repeat.

The screenshot of Scratch code blocks shows 2 commands that needed to be repeated 5 times. Instead of writing those commands 5 times, a repeat loop was used instead. This saves the programmer time and makes it faster to update the code if the programmer wants to change the wait seconds or add an extra block in.

Activity 2

Python allows us to use two main types of loop. A while loop and a for loop.

FOR LOOPS run for a set number of times.

WHILE LOOPS run while a condition is True (do you recognize that word from the last outcome?).

Let's have a look at a For loop in Python. Try this piece of code:

for x in range(1,11):
    print (x)

What happens when you run this code? Can you describe it?

  • x is a temporary variable used within the loop.
  • range(1,11) is the number of times we want the loop to run.
  • print x is printing the value of x.
  • The value of x changes as the variable iterates (loops) through the range.

The video below goes through this step by step to hopefully make it clearer!

Activity 3

for x in range is great when we want to loop through some numbers, but what happens when we want to loop through something else? We could use for item in sequence for this.

Try this:

for letter in "I Love Python":
    print (letter)

Before you click run, try and guess what it is about to do!

In this case the sequence (remember that word from 7_3_1?) is a collection of string. It will iterate through those letters using the temporary variable "letters" and it will print one letter at a time until it reaches the end.

Activity 4

What about our Fibonacci problem from 7_3_1, could we use a for loop for this?

One solution is below. Remember, there are plenty of ways to solve a problem and this isn't necessarily the best way!

fibonacci = 1,1,2,3,5,8,13,21
for number in fibonacci:
    print (number)

The beauty of the algorithm above is that we can now add the next lot of numbers to the end of the fibonacci variable assignment and it will iterate through all of them. This saves the programmer time because she won't have to write "print 34, print ....etc."

Add some more numbers to the fibonacci sequence and see what happens for yourself.

Activity 5

We are going to look at while loops now. While loops run while a condition is True. We know all about conditions and True or False because we learnt about this in the last outcome.

Let's stick with the theme from the last outcome and use a password checking program.

Try this code:

password = "cheese"
passwordTry = ""
passwordIncorrect = (password != passwordTry)
while passwordIncorrect:
    passwordTry = input("Password : ")
    passwordIncorrect = (password != passwordTry)
print ("You are in!")

The important thing here is...do you UNDERSTAND this code? Can you see what it is doing? If not, watch this video. Watch it as many times as you need to:

Activity 6

So a while loop runs while a condition remains True.

Let's just try another one to make sure we understand it...

Try this code:

x = 1
thisIsTrue=True
while thisIsTrue:
    print (thisIsTrue)
    x+=1
    if x==4:
        thisIsTrue = False

Before you run the code, how many times do you think it will print True on the screen?

CHECK:

EMBED:

Option One

Investigate using loops in Scratch.

Option Two

Choose one of the scenarios below to program (or come up with your own idea):

  • A guess the word game that uses a loop
  • A guess the number game that uses a loop
  • A program that flips a coin 5 times and displays the result of each flip

CLASSROOM IDEAS:

The YouTube videos on this page are mainly for students working from home or needing an extra reminder. It is sometimes better to show this kind of explanation to the class to give them time to answer questions and help you to stop any misconceptions.