stdin and Exact Formatting
Lesson 27 of 48 in Coddy's Python for Dart Developers course.
Dart's native stdin.readLineSync returns nullable text at end of input. Python input removes the trailing newline and raises EOFError when none remains. Both require explicit numeric conversion. Dart trim and Python strip remove surrounding whitespace; keep internal spaces in labels. Format integer cents by dividing into whole units and a two-digit remainder, avoiding floating-point rounding. Dart padLeft and Python format specifications can produce the leading zero. In these exercises, the locked driver handles standard input and output around the editable function APIs, so extra prompts or debug prints violate the output contract.
Dart
void main() {
final label = ' Green tea '.trim();
final cents = 305;
print('$label: ${cents ~/ 100}.${(cents % 100).toString().padLeft(2, '0')}');
}Python
label = ' Green tea '.strip()
cents = 305
print(f'{label}: {cents // 100}.{cents % 100:02d}')Porting trap
Do not port native Dart I/O assumptions to a browser environment. This course uses the Python runner's standard input contract.
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 Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler