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)
Iteration Nested
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
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)
for x in range(1,4): for y in range(0,3): print(chr(65+y) + str(x))
x = 1 y = 65 while x <= 3: while y < 68: print(chr(y) + str(x)) y = y + 1 x = x + 1 y = 65
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.for
loop require less lines of code in the above first two examples? Explain it to a mate.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
1 2 1 3 2 1 4 3 2 1 Count: 10 Sum: 20
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: 9Note - you do not need a 'nested loop' to answer this question
A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 A4 B4 C4 D4
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)