Menu
CoddyTech

Suites Instead of end

Lesson 5 of 48 in Coddy's Python for Lua Developers course.

Lua delimits control blocks with keywords such as then and end. Python uses a colon and indentation; dedenting ends the suite. Keep indentation consistent, normally four spaces. Unlike a Lua local declared inside an if block, a Python name assigned in an if suite remains in the surrounding function or module scope. It only exists after the assignment executes. Moving a line changes control flow, so indentation is part of program meaning.

Lua example

local stock = 7
if stock > 5 then
    print("bulk")
end
print("done")

Python

stock = 7
if stock > 5:
    print("bulk")
print("done")

Porting trap

Python has no end keyword to rescue misplaced indentation. A dedented print runs outside the conditional.

Implement the specified API in the editable Python files. The locked driver.py runs the program and supplies input/output. Keep its imports and names unchanged.

challenge icon

Challenge

Beginner

Read a nonnegative integer stock level. Print audit first. If stock is greater than 5, print bulk and then the stock number. Always print done last. Repair the block layout and add the missing output; no other lines should appear.

Print only the requested output, with a newline after each line. The supplied input lines read the test data; keep them and add your work below.

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

Practice on your own: Online Python compiler