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
BeginnerRead 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__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler