Python KPRIDE: Nested Loops

Last updated 7 months ago
7 questions

For GCSE, you must know the following for programming:

  • Outputs and Inputs
  • Variables
  • Maths Operators
  • Random numbers
  • Strings and Casting
  • Booleans
  • If, elif, else
  • Nested Ifs
  • For loops
  • While loops
  • Nested loops
  • Arrays/Lists
  • 2D Lists
  • File handling
  • Subprograms(functions/procedures)

Warm-up Practice:
https://type.withcode.uk/python/Repetition_Iteration

Learning Objectives:

  • I can explain Condition Controlled iteration to someone
  • I can create a While loop
  • I know when to use a While loop

Keywords

  1. Iteration: Lines of code are repeated a set number of times or until a condition is met.
  2. Count Controlled iteration: Code is repeated a number of times
  3. Condition Controlled iteration: Code is repeated until a condition is met
  4. For Loop: A type of Count Controlled loop.
  5. 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 ...

1

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)

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 ")

Investigate

  1. Look at the code on the right.
  2. Run it and see what it does.
  3. Click on the pencil icon at the top to see the code again.

FIRST EXAMPLE:
  1. Run the program and see what it does
  2. Remove the first -1 on the for loop. What happens?
  3. 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.

Debug

Click on each link and try dragging and dropping code into the correct order
https://7strikelink7.github.io/NormalPythonParsons/parsons/NestedLoops.html

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.
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 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.