FAQ: Learn Python: Loops - Continue

This community-built FAQ covers the “Continue” exercise from the lesson “Learn Python: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Continue

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

5 posts were split to a new topic: What is the scope of a variable that is part of a for loop?

So I don’t get this, surely it would be much more efficient to just do

for i in ages:
if i >= 21:
print(i)

3 Likes

But if you did that you wouldn’t learn about the existence continue :slight_smile:

But why isn’t the else statement needed here:

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

?

Can someone please help me understand indentation rules a bit more?
my code looked like this:

ages = [12, 38, 34, 26, 21, 19, 67, 41, 17]
for age in ages:
if age < 21:
continue
print(age)

and it printed: 17

obviously when I indented it correctly I got the right answer but I can’t imagine why it would have printed 17 in this case.

edit: oh dear it doesn’t let me save the indents. basically I had everything indented correctly except the print statement was on the same level as the word for

i dont get it…


shouldnt it be printing all the ages that are more then 21?
becuse age > 21…?

The print statement is never reached when age > 21. Remove continue and indent the print statement.

1 Like

ok but why dose it do the opposite if the statement dont reached?

Because the way it is written above the print statement is not in the if statement.

I’m just curious.

In the example:

numbers = [1, -2, 3, -4, 5, -6, 7, 8, -9, 10]

for p in numbers:

if p >= 0:

continue

print(p)

Why does print(p) action print out negative integers. Once the if statement is set to greater than or equal to 0.

Because it is preceded by continue in the condition, which means the print statement is skipped over except when the numbers are negative.

You certainly could use an else statement, but it is unnecessary, the for loop will just move onto the next line. If statements in general do not require else statement to run