Python KPRIDE: Nested Loops
star
star
star
star
star
Last updated 7 months ago
7 questions
Keywords
- Iteration: Lines of code are repeated a set number of times or until a condition is met.
- Count Controlled iteration: Code is repeated a number of times
- Condition Controlled iteration: Code is repeated until a condition is met
- For Loop: A type of Count Controlled loop.
- Increment: A value to increase by (e.g. + 1)
Nested loops are loops inside a loop.
The inner loop runs completely for each iteration of the outer loop.

1
This part is the ...
This part is the ...
1
This part is the ...
This part is the ...
Predict and Run
Look at the code for each question.
What do you think will be the output?
What is the actual output?
1
What do you think happens in the code below:
for i in range (1,3): print("Outer", i) for j in range (4,6): print ("Inner:",j)
What do you think happens in the code below:
for i in range (1,3):
print("Outer", i)
for j in range (4,6):
print ("Inner:",j)
1
Look at the code below. What do you think will happen if you input "Yellow"
keepGoing = "y"while keepGoing == "y": reverseWord = input("Enter a word to be reversed: ") for letters in range (len(reverseWord)-1,-1 ,-1): print(reverseWord[letters]) keepGoing = input("Keep going? y/n ")
Look at the code below. What do you think will happen if you input "Yellow"
keepGoing = "y"
while keepGoing == "y":
reverseWord = input("Enter a word to be reversed: ")
for letters in range (len(reverseWord)-1,-1 ,-1):
print(reverseWord[letters])
keepGoing = input("Keep going? y/n ")
Investigate
- Look at the code on the right.
- Run it and see what it does.
- Click on the pencil icon at the top to see the code again.
FIRST EXAMPLE:
- Run the program and see what it does
- Remove the first -1 on the for loop. What happens?
- Put the -1 back, then change the second number to 0. What happens?
Note:
Many programs, apps, games are usually in one main loop like a while loop. This is so that it makes it reusable or to know when to stop.
e.g. When HP is 0, we know it's game over.
or to keep looping if the user doesn't enter the right thing.
Extend
Help:
print ( ) is used to output text
input( ) is used to get the user to type something
Variables store values that can change.
Name = Value here
To manipulate "Strings":
len( word ) returns how long it is
.upper() turns it to upper case
.lower() turns it to lower case
variable[x] returns the letter at position x
int( string) turns it to a string (if possible)
float (string) turns it to a float (if possible)
str (variable) turns it to a string " "
Boolean: a data type that can hold one of two values (true or false)
> Symbol: Greater than
< Symbol: Less than
== Symbol: Compares if both values are the same
!= Symbol: Not equal to
>= Symbol: Greater than or equal to
<= Symbol: Less than or equal to
Selection -
if (condition) :
(indent) program here....
Iteration -
for varName in range (start, end, increment):
(indent) program here....
while (condition) :
program here ....
1
Create a program that uses two for loops.
Print the word "Hello" then "Amy" twice.That does this at least twice:
E.g.HelloAmyAmyHello Amy Amy
Create a program that uses two for loops.
Print the word "Hello" then "Amy" twice.
That does this at least twice:
E.g.
Hello
Amy
Amy
Hello
Amy
Amy
1
Create a program that will display the times table of a number the user enters (from x1 to x10). Keep doing this until the user wants to stop.
e.g. Enter a number: User enters 55x1 = 55x2 = 10 5x3 = 15 ....Want to see another times table? Y/n?Repeat if yesStop if no.
Create a program that will display the times table of a number the user enters (from x1 to x10). Keep doing this until the user wants to stop.
e.g.
Enter a number:
User enters 5
5x1 = 5
5x2 = 10
5x3 = 15
....
Want to see another times table? Y/n?
Repeat if yes
Stop if no.
1
Create a program that uses loops to print out all the hours and seconds of a 24 hour clock.
Create a program that uses loops to print out all the hours and seconds of a 24 hour clock.