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.
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
Output
10
e
oe
eoe
oeoe
eoeoe
oeoeoe
eoeoeoe
oeoeoeoe
eoeoeoeoe
oeoeoeoeoe