In an investigate task you take working code apart to see how each piece contributes to the result. Answer every question for yourself first. You can edit the code and run it to check. Only then open the answer.
Run the program a few times before you start. Once with 3 and then 7, once with 5 and then 5, and once with 7 and then 3.
print("Please enter the starting number")
start = int(input())
print("Please enter the ending number")
end = int(input())
for i in range(start, end + 1):
print(i)
Which line does the iteration start on?
Line 7, the line that reads for i in range(start, end + 1):.
The six lines above it get the program ready: they ask for two numbers and store
them. Nothing repeats until the for, and the iteration runs from there to the
end of the indented block below it.
What is the identifier of the variable used as the counter for the loop?
i.
It is named between for and in, which is where the counter is always named.
Nothing creates it beforehand and nothing adds to it inside the loop: the for
line makes it and fills it with a new value on every repetition.
i is a common name for a loop counter in code that does not need a more
descriptive one. Naming it counter would work exactly the same way.
What is the identifier of the variable that sets the first value of the loop counter?
start.
It is the first number inside range(...), so it is the value i holds on the
first repetition. Type 3 when the program asks and the first line of output is
3.
What is the identifier of the variable that sets the stop value of the loop counter?
end.
It is the second number inside range(...). It does not appear on its own
though: what the range is actually given is end + 1, which the next question
is about.
Explain why the end variable needs a + 1 after it.
Because range stops before its stop value, and this program is supposed to
include the number the user typed.
Enter 3 and 7. range(3, 7) would count 3, 4, 5, 6 and stop, so the 7 the user
asked for would never be printed. range(3, 7 + 1) is range(3, 8), which
counts 3, 4, 5, 6, 7.
The + 1 is not a fix for a mistake somewhere else. It is how you say “up to
and including” to a function that otherwise means “up to but not including”.
Explain the impact of changing line 7 to for i in range(start, end + 1, 2).
The counter goes up in twos instead of ones, so the program prints every other number.
Enter 3 and 7 and the output becomes 3, 5, 7 rather than 3, 4, 5, 6, 7. Enter 3 and 8 and it is still 3, 5, 7: the counter would land on 9 next, which is past the stop value, so the loop ends and the 8 is never printed.
That is the thing worth noticing. With a step of 1 the last number printed is always the one the user typed. With a step of 2 it is only printed when the distance between the two numbers happens to be even.
Make sure that you check for the following things:
for.range(...) to work
through.for line has a colon (:) at the end.for, and all of it
by the same amount. Never mix tabs and spaces in one file.int().