0 of 6 problems solved • 0%

Exercise prompt

Parse a Count

Returning None lets callers distinguish a bad reading from the valid number zero.

Example

A train sign can show either a price or a message. Try converting the sign before doing arithmetic.

sign = "closed" try: price = float(sign) print(price * 2) except ValueError: print("Ask at the desk") # printed for "closed"

With a numeric sign, the multiplication runs. A failed conversion jumps straight to the handler.

Your task

Convert a string to an integer using int. Return None if conversion raises ValueError. Signed integers and surrounding spaces should work.

parse_count(" -7 ") # -7 parse_count("7.5") # None