Input and Exact Text
Lesson 27 of 48 in Coddy's Python for Lua Developers course.
Lua io.read commonly returns nil at end of input. Python input returns a string without its final newline and raises EOFError when input is exhausted. strip removes outer whitespace and split without arguments splits on whitespace runs. Python f-strings embed expressions and support formatting such as two-digit zero padding. The exercises keep money as integer cents. Return helper results instead of printing prompts because the locked driver owns the exact input/output contract.
Lua example
local label = (" Green tea "):match("^%s*(.-)%s*$")
local cents = 305
print(string.format("%s: %d.%02d", label, math.floor(cents / 100), cents % 100))Python
label = " Green tea ".strip()
cents = 305
print(f"{label}: {cents // 100}.{cents % 100:02d}")Porting trap
Lua string patterns and Python regular expressions are different languages. Do not port a Lua percent-pattern directly into a Python regex.
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 product label on the first line and a nonnegative integer number of cents on the second. Remove surrounding whitespace from the label but preserve internal spaces. Print exactly label: dollars.cents, with two digits after the decimal point and a final newline. The label may be empty, in which case the output begins with : . Do not print a currency symbol or input prompts.
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 Lua Developers
1From Lua 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