FAQ: Learn Python: Loops - Continue

I have the exact same formatting and layout as the solution with zero differences and only get
17.

When I replace the code with the solution (which is identical) I get the ‘right answer’
Is this broken ???

the indents might be wrong!

excuse me i did all the work on continue, but can you please solve this problem?

ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]

for age in ages:
if age < 21:
continue
print(age)

Great pun!
image

Isn’t skip would have been better instead of continue. It would have be more relatable

Is the way the lesson has us do it more efficient than:

ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]

for age in ages:
if age > 21:
print(age)
else:
continue
I think it’s more understandable to check for the positive condition (where we want to print) than the inverse. If the goal is to be understandable, then I think this is better. But I am not sure if the else statement will slow down processing.

That is redundant. Your loop will run fine with that removed. Remember to include 21 in your condition.

>= 21

weird way of doing that, ‘continue’? just curious, why do you think they(the developers of the language) chose the word ‘continue’ to do what it is doing?

Let’s look at a simple example:

a = range(10)
for x in a:
    if x % 2:
        continue
    print (x)
0
2
4
6
8

Yes, we could configure our range to do the same thing but think in dynamic terms where we do not control the data and it may even be random in nature. It doesn’t even have to be numeric. The type of controlled loop above could have many applications.

The main purpose of the instruction is to drop the current iteration cycle and commence a new one. The loop itself will have a chance to run completely through while skipping some of the iterations based on the conditional we set forth.

i know that, i was asking your thoughts/opinions/theory on the reasoning behind using the word ‘continue’ to skip the current iteration

TBH I have no thoughts, opinions or theories with regard to continue. We have only to know what is promised and then decide on a case by case basis whether it fits our logic, or not. The example above may be over simplified but there is no real reason to complicate things. We know what the instruction does, and have only to apply it, or alternate logic in our controlled loops.

Note above that we could follow the continue by an entire block of code, not just a print statement. Of course our choice of a controlled loop would be notwithstanding since continue only works in for loops, as far as I know. Will need to do some more reading on it.

are you human? ‘no thoughts, opinions or theories’
highlight ‘thoughts’

As mentioned earlier, I have what I need, the promise made by the language interpreter. It does not matter what I think of that since it won’t change anything. Some things just don’t need to be considered beyond learning what is promised. Need we think on this any further?

either you misinterpreted something, or maybe i, or maybe i should just drop this. sure, the topic can be dropped i will leave you alone, sorry if i wasted your time.