Suites Instead of end
Lesson 5 of 48 in Coddy's Python for Luau Developers course.
Luau uses keywords such as then and end to delimit control blocks. Python instead uses a colon followed by an indented suite, conventionally four spaces. Dedenting ends the suite. A Luau local declared inside a block has block scope; a Python assignment inside an if belongs to its surrounding function or module and only exists after that branch executes. Indentation therefore changes program behavior rather than serving as decoration.
Luau 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
BeginnerRead 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__")
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Python for Luau Developers
1From Luau to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler