Menu
CoddyTech

Input and Exact Output

Lesson 27 of 48 in Coddy's Python for C# Developers course.

Parse text input and format exact output without prompts.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

string label = "  tea box  ".Trim();
int cents = 205;
Console.WriteLine($"{label}: {cents / 100}.{cents % 100:D2}");

Python

label = "  tea box  ".strip()
cents = 205
print(f"{label}: {cents // 100}.{cents % 100:02d}")

What changes

input reads one text line and removes the line ending. Unlike Console.ReadLine returning null at end of input, input raises EOFError. Here input is supplied by the driver or challenge, so no prompt text is wanted. strip removes surrounding whitespace while preserving spaces within a label. Format integer cents without converting money to float.

Transfer mistake

Python f-string format :02d gives a minimum two-digit integer field. It is not C# :D2 syntax. Do not use locale-dependent currency formatting when the output contract names a literal colon and decimal point.

Your task

Read a product label and nonnegative integer cents on two lines. Trim only surrounding label whitespace. Output label: dollars.cents, using exactly two digits after the decimal point and a final newline. Internal label spaces remain. An empty label makes the line begin : . No prompts or currency symbols.

challenge icon

Challenge

Beginner

Read a product label and nonnegative integer cents on two lines. Trim only surrounding label whitespace. Output label: dollars.cents, using exactly two digits after the decimal point and a final newline. Internal label spaces remain. An empty label makes the line begin : . No prompts or currency symbols.

Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.

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 C# Developers

Practice on your own: Online Python compiler