Input and Output Contracts
Lesson 27 of 48 in Coddy's Python for JavaScript Developers course.
Turn a documented input line into values and produce exactly the output another program expects.
JavaScript
const line = " 6 4 ";
const [a, b] = line.trim().split(/\s+/).map(Number);
console.log(`${a + b} units`);Python
line = " 6 4 "
left, right = line.split()
a, b = int(left), int(right)
print(f"{a + b} units")What changes
Both examples print 10 units. Their fixed line stands in for data supplied by an input system. Python input() reads one line and returns text without its trailing newline; it does not automatically parse numbers. Calling split() without an argument separates runs of whitespace and omits empty pieces at the edges. Calling split(",") instead uses a literal separator and retains empty fields. Use strip() when surrounding whitespace is irrelevant to an entire value. An f-string places expressions inside braces. A format specifier such as :02d displays an integer using at least two digits with leading zeros; :.2f displays two digits after the decimal point. These are display rules, not changes to the underlying values.
Common mistake
Do not add a prompt to input in an automated challenge unless requested. That prompt becomes extra output. Preserve empty lines when the contract gives them meaning, and separate parsing from formatting so an accidental space cannot change the result.
Your task
Read two lines using their stated contracts and format integer cents without relying on floating-point arithmetic.
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 JavaScript Developers
1From JavaScript to Python
Run Your First Python ProgramPrint, Comments, and StringsVariables Without let or constNumbers and ConversionBlocks Use IndentationRecap: Rewrite a Receipt4Iteration and Transformations
for Loops Iterate ValuesIndexes and Dict Iterationwhile, break, and continueComprehensions and SortingRecap: Build a Ranking2Values and Decisions
None, True, and FalseEquality and IdentityTruthiness and Short-CircuitBranches and ExpressionsRecap: Eligibility Rules3Collections and References
Lists Instead of ArraysTuples and UnpackingDictionaries and RecordsSets, Aliasing, and CopiesRecap: Clean an Inventory6Reliable Python Programs
Input and Output ContractsExceptions and ValidationModules and the Main GuardReading Type HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler