External Exam Download Resources Web Applications Games Recycle Bin

Iteration Nested

nested loop.py

outer_loop_counter = 3
inner_loop_counter = 2

while outer_loop_counter >= 1:
  print("Outer loop counter:", outer_loop_counter)

  while inner_loop_counter >= 1:
    print("--> Inner loop counter:", inner_loop_counter)
    inner_loop_counter = inner_loop_counter - 1

  outer_loop_counter = outer_loop_counter - 1
  inner_loop_counter = inner_loop_counter = 2


nested for loop.py

for outer_loop_counter in range(3,0,-1):
  print("Outer loop counter:", outer_loop_counter)
  for inner_loop_counter in range(2,0,-1):
    print("--> Inner loop counter:", inner_loop_counter)


nested for ENCRYPTION.py

for x in range(1,4):
  for y in range(0,3):
    print(chr(65+y) + str(x))


nested while ENCRYPTION.py

x = 1
y = 65

while x <= 3:
  while y < 68:
    print(chr(y) + str(x))
    y = y + 1
  x = x + 1
  y = 65


  1. How do break and continue work with nested loops? Do they break or continue out of both inner and outer loops? Try it yourself and explain it to your mate.

  2. Why does the for loop require less lines of code in the above first two examples? Explain it to a mate.

  3. Re-write the code above so that it produces this output:
    Outer loop counter: 0
    --> Inner loop counter: 0
    --> Inner loop counter: 1
    --> Inner loop counter: 2
    Outer loop counter: 2
    --> Inner loop counter: 0
    --> Inner loop counter: 1
    --> Inner loop counter: 2
    Outer loop counter: 4
    --> Inner loop counter: 0
    --> Inner loop counter: 1
    --> Inner loop counter: 2

  4. Write a program that counts upwards from 0 to N (where N is an integer entered by the user). For each number, the program should print all the numbers counting back down to 0. At the end of the program, the sum and count of all numbers printed should be displayed. For example, if the user inputs N as 5, the output should be:
    1 
    2 
    1 
    3 
    2 
    1 
    4 
    3 
    2 
    1 
    Count: 10 
    Sum: 20

  5. There are an unknown number of students in the class that completed the 'beep' test in HPE (a test of fitness). The students run until they can't run any further, with their progress measured in levels. The levels in which the students score range betwen 5 and 15 (in reality the 'beep' test uses sub-levels, but for this program, you can treat the levels as whole integers). Write a program that reads in the number of students and their level scores. From this - calculate the highest, lowest and average. Your program might look something like this:
    Number of students: 7
    Student 1 level: 14
    Student 2 level: 7
    Student 3 level: 7
    Student 4 level: 7
    Student 5 level: 6
    Student 6 level: 11
    Student 7 level: 11
    Minimum level score: 7 
    Maximum level score: 14
    Average level score: 9
    Note - you do not need a 'nested loop' to answer this question


  6. Let me enter a grid size for a game of battleships. Depending on the grid size (which is variable), let me print out the grid as follows:
    A1 B1 C1 D1
    A2 B2 C2 D2
    A3 B3 C3 D3
    A4 B4 C4 D4


battleships.py

ASCII_A = 65 #character 65 is letter 'A'
width = int(input("Grid width: "))
for y in range(1, width + 1, 1):
    line = ""
    for x in range(ASCII_A, ASCII_A + width, 1):
        line += chr(x) + str(y) + " "
    print(line)