We can use for loops with any iterable like range():
for index in range(5):
print(f'Input the next number {index}:')
n = int(input())
print(f'Great! you\'ve entered {n}')
index
User Input:
Program output
0
7
Input the next number 0:
Great! you've entered 7
1
9
Input the next number 1:
Great! you've entered 9
2
-4
Input the next number 2:
Great! you've entered -4
3
8
Input the next number 3:
Great! you've entered 8
4
0
Input the next number 4:
Great! you've entered 0
In this example, the index variable first took the value 0, then the block of code inside the for-statement was executed, then the index took the value 1, then 2, 3, and finally 4.
for loops are used together with range() quite often and we will see their use together more frequently in the future.
Challenge
You are asked to create a countdown that starts from a certain number of goes down up to 0.
The input contains a single positive integer n. The program should print all the numbers for the countdown, each on a new line.