Menu
CoddyTech

Recap: Validate a Batch

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

challenge icon

Challenge

Beginner

Read nonnegative n and exactly n raw record lines. A valid record has two comma-separated fields: a nonempty name and an integer quantity in [0, 100]. Strip surrounding field whitespace but keep internal name spaces. For valid rows print name:quantity; otherwise print error:position using one-based record position. Continue in input order and retain duplicate names. Finally print accepted=A rejected=R total=T with actual counts and accepted quantity sum. Catch expected conversion failures and validate shape explicitly.

C# transfer checkpoint

A C# parsing exception maps to a specific Python conversion failure here; do not catch every programming error.

C# reference excerpt: executable statements assume using System; type-only excerpts illustrate the declared contract.

try { int.Parse("bad"); }
catch (FormatException) { Console.WriteLine("invalid"); }

Python pattern

try:
    int("bad")
except ValueError:
    print("invalid")

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__")

All lessons in Python for C# Developers

Practice on your own: Online Python compiler