python question from Blaise Carrington, a Cameroonian living in Equatorial Guinea.
![](https://secure.gravatar.com/avatar/9a2e5ebd4e780e2801ec3cf8816998eb.jpg?s=120&d=mm&r=g)
Good day, sorry for any inconveniences. I wish to get an explanation concerning the first code in section 4.4 of the python manual... 4.4 break and continue statements, and else clauses in loops... according to my understanding of the code, I figured the break applies to values that go thru the if function, so I was wondering, for the number 5 o 7, they should always fail the condition of the if function and hence always break, how do the eventually get to the else part? pleasw a response will do a lot to my tiny brain... thank you.
![](https://secure.gravatar.com/avatar/9a2e5ebd4e780e2801ec3cf8816998eb.jpg?s=120&d=mm&r=g)
Good day, I got confused writing the first question I sent, what I meant to say is: the number 9, on dividing by 2, it gives a remainder of 1, hence won't be subjected to the if loop and as such has to move directly to the else part, what takes it past the else part to continue to the if part with x taking the integer 3? On Wed, Apr 29, 2020, 2:36 PM Oliver Carrington <olicarra237@gmail.com> wrote:
Good day, sorry for any inconveniences. I wish to get an explanation concerning the first code in section 4.4 of the python manual... 4.4 break and continue statements, and else clauses in loops... according to my understanding of the code, I figured the break applies to values that go thru the if function, so I was wondering, for the number 5 o 7, they should always fail the condition of the if function and hence always break, how do the eventually get to the else part? pleasw a response will do a lot to my tiny brain... thank you.
![](https://secure.gravatar.com/avatar/d8798382aa64d5767d749a8328364e53.jpg?s=120&d=mm&r=g)
Hi Oliver! This list is not intended to get help about Python, just to report issues or discuss enhancements about its documentation. If you feel this question is induced by something that could have been explained better in the doc, feel free to let me know and proponse enhancements. About your question, looks like you missed the fact that the else is at the same level as the for, not the same level as the if. So the else is bound to the for, not the if, which looks surprising if you don't know Python yet. But as 4.4 is about "else Clauses on Loops" we're right on the topic. About 9 being found to equal 3 × 3 ahere's the flow: - We're on the n = 9 loop of the first for, so n = 9 during the whole explanation - x get assigned to 2, as you said there's a reminder of 1, so the if block is not executed. No jump to an "else" as there is no "else" for this if (remember: this else is at the for level). - x get assigned to 3, 9 % 3 equals zero so the if block is executed, the print prints, and the loop breaks. As the loop exited with a break, the else block is *not* executed, the last print is not printed (9 is not prime). Hope it helps, Bests, -- Julien Palard
![](https://secure.gravatar.com/avatar/9a2e5ebd4e780e2801ec3cf8816998eb.jpg?s=120&d=mm&r=g)
Awesome, I'm grateful. Thanks for the clarification, you're the best... On Tue, May 5, 2020, 10:28 PM Julien Palard <julien@palard.fr> wrote:
Hi Oliver!
This list is not intended to get help about Python, just to report issues or discuss enhancements about its documentation. If you feel this question is induced by something that could have been explained better in the doc, feel free to let me know and proponse enhancements.
About your question, looks like you missed the fact that the else is at the same level as the for, not the same level as the if. So the else is bound to the for, not the if, which looks surprising if you don't know Python yet.
But as 4.4 is about "else Clauses on Loops" we're right on the topic.
About 9 being found to equal 3 × 3 ahere's the flow:
- We're on the n = 9 loop of the first for, so n = 9 during the whole explanation - x get assigned to 2, as you said there's a reminder of 1, so the if block is not executed. No jump to an "else" as there is no "else" for this if (remember: this else is at the for level). - x get assigned to 3, 9 % 3 equals zero so the if block is executed, the print prints, and the loop breaks. As the loop exited with a break, the else block is *not* executed, the last print is not printed (9 is not prime).
Hope it helps, Bests, -- Julien Palard
participants (2)
-
Julien Palard
-
Oliver Carrington