AI Agents by HandAll problemsAbout

All problems › Python drill · easy · ~5 min

d08 · Parse a quantity

parsing input validation

Solve it in your browser →

d08 · Parse a quantity (~5 min)

Parsing strings. Edit drills.py. Standard library only; no model calls, no API key.

def parse_quantity(value):

Return a positive whole number from a messy tool input.

Accept: an int; a float or string holding a whole number (3.0, "3", " 3 ", "3.0"); a string with a number followed by whitespace and words ("3 units"); full-width digits ("3"). Raise ValueError for anything else, including 0, negatives, non-whole numbers (2.5, "2.5"), booleans, None, "", "three" and "3abc".

Failure messages are enough to go on, so leave the tests unread until you submit.

Starter code and hidden tests open in the editor. Nothing to install.