Menu
CoddyTech

While Without Increment

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

Python while loops, break, and continue handle repeated work much like PHP's basic loops. Python break exits the nearest loop; PHP's numeric break levels have no direct Python spelling. Update counters with += 1. Python has no increment operator: ++n is two unary plus operations and does not modify n. Place a while-loop update before any continue path that would otherwise skip it.

PHP 8.3

<?php
$n = 1;
++$n;
echo $n, "\n";

Python

n = 1
++n
print(n)

Porting trap

A direct port of PHP ++$n can become a legal Python expression that fails to advance the loop. That can create an infinite loop rather than a syntax error.

Complete the task in the editable Python files. Keep the required function and class names; the locked driver.py supplies execution.

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

Practice on your own: Online Python compiler