Menu
CoddyTech

Loop Updates and continue

Lesson 19 of 48 in Coddy's Python for Dart Developers course.

Dart and Python both provide while, break, and continue. A continue skips the remaining current iteration; a break exits the nearest loop. Python has no increment operator, so write n += 1 rather than n++. A while loop needs a state update on every path that should advance. Place updates before a continue when skipping them would prevent termination. Python for advances its iterator automatically and has no update clause to port from Dart's counted for syntax. The task reads records until a sentinel and skips negative values while continuing to consume input.

Dart

void main() {
  var n = 0;
  while (n < 3) {
    n++;
    if (n == 2) continue;
    print(n);
  }
}

Python

n = 0
while n < 3:
    n += 1
    if n == 2:
        continue
    print(n)

Porting trap

Python double unary plus is not increment, and postfix increment is not Python syntax. Use explicit augmented assignment.

challenge icon

Challenge

Beginner

Read one line at a time until the exact line stop, which is guaranteed to occur. Other lines are integers between -1000 and 1000. Ignore negative readings. Zero is valid. Print the count and sum of accepted readings on one line separated by a space. Ignore any input after stop.

Your code goes in main.py. The locked driver.py starts your program and cannot be edited.

Try it yourself

# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module

run_module("main", run_name="__main__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for Dart Developers

Practice on your own: Online Python compiler