07 - Introduction To Python - Looping
07 - Introduction To Python - Looping
Programming
Part 7: Looping
How to use this resource
Skill Explanations: Pink Slides
• Within each unit, you will learn how to develop
and apply a range of Python programming skills.
Skill explanations are in pink.
Tasks: Rookie Pro Beast
• After reading the explanation of a skill, apply it
within the given tasks. The tasks are categorised
into Rookie (Easy), Pro (Medium) or Beast (Hard).
Challenges: Rookie Pro Beast
• Once you have learned how to apply your new
skills, demonstrate your ability by completing the
given challenges. Don’t forget the debugging task!
• Once complete, review your performance!
Learning Objectives
Rookie
• To be able to write a simple While loop.
• To be able to write a simple For loop.
Pro
• To use both While and For loops to make coding more efficient.
Beast
• To use loops to check for and compare information.
Skill Contents
Once you complete a skill, colour code
Contents Confidence
the box to show your level of Level
confidence. You can always revisit the While loops
skill later on to boost your confidence.
For loops
Key Colour For loops: add a starting number
Code
For loops: increment the number
I am very good at this skill by more than 1
and could show a friend
how to do it. For loops: comparing data
I am confident I can
perform this skill on my
own.
I need to improve my
understanding of this skill.
What is Looping (Iteration)?
Why write all this code:
Start
WHILE the condition is TRUE:
Perform the task(s)
End START
tired = FALSE
Note how the task to Do WHILE tired = FALSE:
press- perform a press-up
be performed WHILE ups tired = user INPUT( )
the condition is TRUE END WHILE
is indented below the END
while code.
Skill 1: Writing while Loops in Python
In Python, a While loop is structured as follows:
START
quit = n
WHILE quit = n:
OUTPUT: Do you want to quit (y/n)?
quit = user INPUT( )
END WHILE
END
Task 1: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 2: Rookie
1. Create a variable called number and assign it the value 1.
2. Using a WHILE loop, print out the numbers 1 to 10 by incrementing the value of the variable number.
START
number = 1
WHILE number < 11:
OUTPUT: number
number = number + 1
END WHILE
END
Task 2: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 3: Pro
1. Create a variable called password. Assign it the password ilovepython.
2. Allow the user to input a password. Use a WHILE loop so that the user is allowed to retype the password until they get
it correct.
3. When the correct password is input, an appropriate success message is displayed and the programme ends.
START
password = ilovepython
WHILE guess ≠ password:
OUTPUT: Please enter the password.
guess = INPUT ( )
IF guess ≠ password:
OUTPUT: Access denied!
ELSE:
END IF
END WHILE
OUTPUT: Password accepted. Welcome back user!
END
Task 3: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 4: Pro
1. Create a variable called number and store a number between 1 and 10 in it.
2. Using a While loop, ask the user to guess your number until they get it right.
3. Make sure you output suitable messages to let the user know if they guessed correctly or not.
4. EXTENSION: Add an option for the user to quit guessing and end the programme.
START
number = [programmers choice]
guess = 0
WHILE guess ≠ number:
OUTPUT: Guess what number I am thinking of!
guess = INPUT ( )
IF guess = number:
OUTPUT: Correct!
ELSE:
OUTPUT: Wrong! Guess again!
END IF
END WHILE
END
Task 4: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 5: Beast
1. Create a variable called number and assign it the value 1.
2. Using a FOR loop, print out the numbers 1 to 10 by incrementing the value of the variable number.
START
OUTPUT: The incredible times table algorithm!
OUTPUT: Enter a number
number = user INPUT( )
i=1
WHILE i < 13
Sum = number x i
OUTPUT: Sum
i=i+1
END WHILE
END
Task 5: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 6: Beast
Extend the guess the number algorithm by adding in an option for the user to quit guessing and end the programme.
START
number = [programmers choice]
QUIT = n
guess = 0
WHILE QUIT = n:
IF guess ≠ number:
OUTPUT: Guess what number I am thinking of!
guess = INPUT ( )
IF guess = number:
OUTPUT: Correct!
ELSE:
OUTPUT: Wrong! Do you want to quit(y/n)?
QUIT = INPUT ( )
END IF
ELSE:
END IF
END WHILE
END
Task 6: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Here is a sporting for loop example!
Skill 2: For Loops
The next type of loop we
will look at is a For Loop.
A For Loop is used to
repeat a set of
instructions a specific
number of times. START
Perform 20 FOR i 1 TO 20:
press-ups Perform a press-up
END FOR
END
In total, a for loop range can take three integer arguments as follows:
START
FOR i 1 TO 10:
OUTPUT: Hello World!
END FOR
END
Task 7: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 8: Pro
1. Ask a user how many numbers they want to add to a list.
2. Use a for loop and the append function to allow a user to enter their chosen number of numbers.
3. Output the finished list.
START
number = user INPUT( )
numbers = [ ]
FOR i 1 TO number
numbers = user INPUT
END FOR
OUTPUT: numbers
END
Task 8: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Task 9: Beast
1. Using a for loop, allow a user to input 10 different scores of their choice into an array.
2. Once all the scores have been input, OUTPUT the array.
3. Extension: Ask the user how many scores they wish to enter.
START
score = [ ]
FOR i 1 TO 10
score = user INPUT( )
END FOR
OUTPUT: score
END
Task 9: Beast
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 3: Adding a starting number within a FOR loop
A FOR loop automatically starts at 0 unless otherwise stated within the range arguments.
([starting number], [up to but not including])
The range function allows you to In the code, (1,11) means i will start with the value 1 and
state how many times you wish the code will continue to repeat until i becomes 11.
the code to be repeated by Therefore, in this example, the code will repeat 10 times.
assigning values to the counter
variable i.
START
FOR i 2 TO 12:
OUTPUT: For loops are easy!
END FOR
END
Task 10: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 4: Incrementing the counter by more than 1
A FOR loop counter automatically increments by 1 step unless otherwise stated.
START
FOR i 2 TO 25:
OUTPUT: i
i=i+2
END FOR
END
Task 11: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Skill 5: Using for loops to compare data
• A for loop can be used to check for specific information within a variable or a list.
• To do this, you will need to understand the following functions:
• Remember: In Python, the first element of a data structure has an index of zero.
• In the example above, the length of the string is 11 characters (note the space is also a
character), however the index range is 0 to 10.
• This means index 0 stores the character H while index 10 stores the character D.
• If we use a FOR loop to check through each index, we need to adjust the FOR loop ranges
as follows:
FOR i 0 TO LEN(list)-1 Final value will be 1
less than the length
First count value will be 0. of the array / string.
Task 12: Beast
1. Ask the user to enter a password. Store it in a variable.
2. Use a for loop and check that it has at least 2 uppercase letters within it.
3. If it does, let the user know their password was accepted, if not, let them know their password was denied.
LIST = [1,2,3] Declare your list and Assign a variable. For example,
either enter the data yourself or allow a user to enter a
it.
For i in variable Use the range function to repeat the check for as long
length: as the length of the checked word.
START
list = [ A, E, I, O, U ]
name = user INPUT( )
vowels = 0
FOR i 0 TO LEN(name)-1:
IF name[ i ] is in list:
vowels = vowels + 1
ELSE:
END IF
END FOR
OUTPUT: Hello + name + you have + vowels + vowels in your name!
END
Task 13: Solution
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Programming Challenges
• Now you have learned some programming skills, it’s time to
put them into practice!
• Complete the following programming challenges. Start with
the Rookie challenge and see how far you can push yourself.
• Finally, complete the debugging challenge.
• Once you have finished your challenges, complete the
review. Look back at the skills you have learned so far to help
you solve the challenge.
Add a print screen of
your solutions to the
Programming Challenge: Rookie next slide.
1 Mark
Programming Challenge: Rookie - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Add a print screen of
your solutions to the
Programming Challenge: Pro next slide.
1 Mark
Programming Challenge: Pro - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Add a print screen of
your solutions to the
Programming Challenge: Beast next slide.
1. Create a variable which stores the name of your favourite sporting hero.
2. Ask the user to guess your sporting hero.
3. If the user guesses the correct name, congratulate them and end the programme. If they
guess incorrectly, allow them to have another go if they wish.
4. If they guess incorrectly, give them a clue; let the user know how many letters in their guess
were the same as in your answer.
5. Give them another clue: let the user know the first letter of your sporting hero’s name, the
next guess tell them the first and second, etc.
6. Make sure your coding is robust.
1 Mark
Programming Challenge: Beast - Answer
• Add a print screen to show • Add a print screen to show
your coding here. your output solution here.
Debugging Question
Which piece of code below would output the phrase below 10 times;
Looping is easy!
1 Mark
Complete the table below to help you identify what
Challenges Review areas you are confident at and which areas you need
to improve further:
Challenge Your Score (Max 1 mark each)
Rookie Challenge
Pro Challenge
Beast Challenge
Debugging Challenge
Unit Review
What Went Well: