Menu
CoddyTech

Updates and Loop Control

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

Kotlin supports increment operators and labeled break or continue. Python uses += for an update and its plain break/continue act on the nearest loop. Python ++n is legal as two unary plus operations but does not increment. Place the update before any continue path that would otherwise bypass it in a while loop. A Python for loop advances its iterator automatically.

Kotlin

fun main() {
    var n = 1
    ++n
    println(n)
}

Python

n = 1
++n
print(n)

Porting trap

A port that keeps ++n may loop forever. Replace the operation explicitly instead of relying on a syntax error to reveal it.

Complete the task in its editable modules. The locked driver.py supplies execution; keep the required API names.

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 Kotlin Developers

Practice on your own: Online Python compiler