Lesson 14 of 34

break and continue

8 min 30 XP

break stops the whole loop immediately. continue skips the rest of this round and moves to the next one.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

while True means “keep going forever” until a break. It's handy for reading input until the user types “stop”.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Challenge

+30 XP

Read numbers one by one with input(). Stop when you get 0 (break). Skip negative numbers (continue) and, at the end, print the sum of the positive numbers only.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.