How to print specific elements from a list that meet a certain condition?

Hi - i’m basically stuck and my class lecture slides and textbook is not helping.

I have “a” as my list of numbers. I got it to print anything that’s less than or equal to 34 but need to also print if it’s 89. I can’t seem to get the 89 in. I’ve tried:

for n in a:
print(n)
if n == 34:
continue
if n == 89:
break

HOWEVER, that prints all numbers in the list from 34-89. I’m totally stuck. Any help or tip would be appreciated.

What do you expect continue to do? When n is equal to 34, Python will continue. Similarly, what do you expect break to do?

Chris, if you’ve only ever seen Jupyter worksheets before, and you haven’t encountered source code written in any (other) programming language yet, then perhaps you honestly haven’t realised, that in the non-Jupyter world (i.e. without those numbers [113], [114], [115] in the left hand margin), it’s taken for granted, that the execution order of statements in a program is from top to bottom.

This doesn’t match the code that you show in the screenshot, so I’m a bit confused.

But anyway, please think carefully about the logic. Instead of trying to control whether the loop exits, or starts again from the top - what if you try using the if to decide whether to do the print call?

2 Likes