code: TU07 points: 12
Loops are easy to do in Python and if you need to do something a certain number of times, like count to 100 then you can do it with a function called for
for i in range (1,100):
print(i)
Exercise 1: Why does it only count to 99? Adjust the code to count and print to 100.
for i in range (1,100):
print(i)
Exercise 1: Why does it only count to 99? Adjust the code to count and print to 100.
Jump a step
Sometimes you don't want to simply count up by 1, but count in steps of 2 or even more...
for i in range (1,100,2):
print(i)
Exercise 2: Count the even numbers between 0 & 100.
for i in range (1,100,2):
print(i)
Exercise 2: Count the even numbers between 0 & 100.
COUNTDOWN
Sometimes you may not even want to count up, but down!
for i in range (100,0,-1):
print(i)
There are a few ways to do this, but the easiest is to start with the top number, then the bottom number followed by the step.
Exercise 3: Try counting down in steps of 10 from 1,000
LIMITATION: THIS FUNCTION WORKS BEST WITH WHOLE NUMBERS
for i in range (100,0,-1):
print(i)
There are a few ways to do this, but the easiest is to start with the top number, then the bottom number followed by the step.
Exercise 3: Try counting down in steps of 10 from 1,000
LIMITATION: THIS FUNCTION WORKS BEST WITH WHOLE NUMBERS