Text Input and Exact Output
Lesson 27 of 48 in Coddy's Python for C Developers course.
C fgets keeps a newline when one fits in its buffer, while Python input removes the line terminator and returns a string. Convert numeric text explicitly. strip removes surrounding whitespace but preserves internal spaces; apply it only where the contract allows trimming. An f-string inserts expressions inside braces and can format an integer remainder with two digits. For nonnegative integer cents, // and % produce dollars and the remaining cents without floating-point rounding.
C: complete example.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
int main(void) {
int cents = 507;
printf("fee: %d.%02d\n", cents / 100, cents % 100);
return 0;
}Python: complete example.py
cents = 507
print(f"fee: {cents // 100}.{cents % 100:02d}")Watch the difference
Formatting 7 with two digits gives 07, not a floating-point number. Do not print prompts in graded tasks. The driver and task statement specify which file owns input; helper functions should not consume additional lines.
Your code goes in main.py. The locked driver.py is the entry file, supplies execution and checks, and cannot be edited.
Challenge
BeginnerRead a label and a nonnegative cents amount from two lines. Trim only the label's surrounding whitespace, keeping internal spaces. Output label: dollars.cents with exactly two fractional digits. An empty label yields a line beginning : . Omit prompts and currency symbols.
Keep the supplied input scaffold. Output only the requested results, with a newline after each line and no prompts.
The Python format specifier 02d plays the zero-padding role of C %02d.
Your code goes in main.py. The locked driver.py is the entry file, supplies execution and checks, 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 C Developers
1From C Programs to Python
Run Python Without mainPrinting Without Format CodesNames Versus Typed StorageDivision and Integer LimitsSuites Instead of BracesRecap: A Python Receipt4Iteration and Transformations
Iteration Without PointersIndexes Without Pointer MathSentinels and Loop ControlComprehensions and SortingRecap: Stable Ranking2Values and Truth Tests
Absence Is Not a Null PointerContent Versus Object IdentityTruth Tests and OperandsConditional Value SelectionRecap: Missing or Eligible3Containers and Aliasing
Lists Versus C ArraysTuples and Struct ValuesMappings and Lookup LoopsPointers, Copies, and AliasesRecap: Inventory Collections6Contracts, Errors, and Modules
Text Input and Exact OutputError Codes Versus ExceptionsImports Are Not IncludesAnnotations Versus C TypesRecap: Validate a Batch9Project: An Order Processor
Project: Model an OrderProject: Pricing PoliciesPractice on your own: Online Python compiler