fb pixel

Nested loops

Just like we can have nested conditions with several nested if and else statements, we can also have nested for and while loops.
We can iterate over time in a day by iterating over hours, minutes, and seconds:
for h in range(24):
	for m in range(60):
		for s in range(60):
			print(f'Time: {h}:{m}:{s}')
The output of the program will be all the possible timestamps of the day as demonstrated on the right.
The for loop starts with h = 0, then uses m = 0, then iterates over all the seconds from 0 to 60, and prints the time.
Then, the program sets m = 1 and iterates over all the seconds. Then it sets m = 2, and repeats this process until reaching m = 59. At this point, the program finishes the second loop and h becomes 1. Then, the whole process of iterating over minutes and seconds for h = 1 repeats again. h then becomes 2, then 3, and the cycle continues until h reaches 23, m reaches 59 and s reaches 59. That stops all the 3 loops together.
Time: 0:0:0
Time: 0:0:1
Time: 0:0:2
Time: 0:0:3
Time: 0:0:4
Time: 0:0:5
Time: 0:0:6
Time: 0:0:7
...
Time: 0:9:20
Time: 0:9:21
Time: 0:9:22
Time: 0:9:23
Time: 0:9:24
...
Time: 23:59:54
Time: 23:59:55
Time: 23:59:56
Time: 23:59:57
Time: 23:59:58
Time: 23:59:59
We can also print more fine-grained objects on the screen with nested loops:
for i in range(10):
	for j in range(i + 1):
		if j % 2 == 0:
			print('*', end='')
		else:
			print('#', end='')
	print()
*
*#
*#*
*#*#
*#*#*
*#*#*#
*#*#*#*
*#*#*#*#
*#*#*#*#*
*#*#*#*#*#
For each line i, we print with an inner loop of j in range(i + 1). The symbol is determined based on the location.

Challenge

Inspired by 2D games, you would like to experiment with printing some shapes. You want to print a triangle of height n and width n, where for each location, the printed symbol will be e if the sum of its row and column is even and o if the sum is odd.
Note that rows and columns start counting from 1.
Input
10

Output
e
oe
eoe
oeoe
eoeoe
oeoeoe
eoeoeoe
oeoeoeoe
eoeoeoeoe
oeoeoeoeoe
 
To check your solution you need to sign in
Sign in to continue