0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
No active XP Potion
Accept a Quest
Login to submit answers
Loops are a programmer's best friend. Loops allow us to do the same operation multiple times without having to write it explicitly each time.
For example, let's pretend I want to print the numbers 0-9.
I could do this:
print(0)
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
Even so, it would save me a lot of time typing to use a loop. Especially if I wanted to do the same thing one thousand or one million times.
A "for loop" in Python is written like this:
for i in range(0, 10):
print(i)
i
replaces the numbers 0
to 9
. In English, the code says:
i
equals 0
. (i in range(0)
)i
is not less than 10 (range(0, 10)
), exit the loop. Else:
i
to the console. (print(i)
)1
to i
. (range
defaults to incrementing by 1)2
.The result is that the numbers 0-9
are logged to the console in order.
The numbers a
, b
in range(a, b)
are inclusive of a
and exclusive of b
.
So range(0, 10)
includes 0
but not 10
.
The body of a for-loop must be indented, otherwise you'll get a syntax error.
Complete the missing sections of the for-loop in the print_numbers
function so that it prints the numbers 0-99 to the console.
Focus Editor
Alt+Shift+]
Become a member to Submit
Become a member to Run
Become a member to view solution