0

The following code is from my homework. I have the question and I also have the answer, but I don't understand how to get the answer.

#Question
result = 0
for n in range(3):
 print(n, end=' ')
 result -= 3
else:
 print('| {}'.format(result))
print('done')
#Answer
0 1 2 | -9
done

The for loop is using a range of 0-2, so I expect it to run through the loop all 3 times and produce the '0 1 2' part, but why does it add the 'else' portion? Shouldn't it only use the 'else' portion if the loop statement evaluates as 'False' but how is it false if the loop knows to only run 3 times and none of the first 3 statements evaluate as false? Or does the else always evaluate when it's a loop-else? I assume I'm missing some fundamental knowledge here. TIA

asked Jun 14, 2022 at 4:08
5
  • its simple like if else , while your loop with statements body ends then your else comes out . its evaluate as break point Commented Jun 14, 2022 at 4:12
  • The else keyword in a for loop specifies a block of code to be executed when the loop is finished: Commented Jun 14, 2022 at 4:12
  • "Shouldn't it only use the 'else' portion if the loop statement evaluates as 'False'". The "loop statement" is an iterable, which can be falsy (if it is empty) but it isn't ever False. Commented Jun 14, 2022 at 4:14
  • 1
    Does this answer your question? Why does python use 'else' after for and while loops? Commented Jun 14, 2022 at 4:16
  • @Kraigolas it doesn't answer it directly as most of the examples were for loops that could be evaluated as false (ie if it didn't find the object/flag) but the general conversation is the same and I did learn some helpful info. Thank you for finding that! Commented Jun 14, 2022 at 4:30

2 Answers 2

2

We use else with for loops to check that if the for loop has reached the end without a break for exmaple:

for i in range(5):
 print(i)
 if (i == 3):
 break
else:
 print("iterated over the whole range")

this will only print numbers from 0 to 3 and then stop without printing the message because we have broke out of the loop.

note that not many programming languages support putting else after a loop.

answered Jun 14, 2022 at 4:18
1

A for loop's else clause is executed when all iterations finish executing without interruption (i.e.: without encountering a break).

If a break is encountered within the for loop, the else won't be executed.

See the Python documentation: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

answered Jun 14, 2022 at 4:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.