Menu
CoddyTech

While and Early Exit

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

GDScript and Python use while, break and continue for repeated work. Update a counter with += rather than expecting an increment operator. In Python, ++n happens to parse as two unary plus operations and does not update the binding. Put progress updates where a continue cannot bypass them forever. A for iterator advances automatically.

GDScript

extends SceneTree
func _init():
    var n = 1
    n += 1
    print(n)
    quit()

Python

n = 1
++n
print(n)

Porting trap

Do not add frame-based assumptions to a Python loop. These command-line tasks run synchronously to completion, without awaiting a Godot process_frame signal.

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

Practice on your own: Online Python compiler