question 174 Exceptions & Validation · easy
Opening hours that make sense
Store managers type opening times into a form as HH:MM. Convert a time to minutes after midnight, and refuse anything that isn't a real time with a message the manager can act on.
Function
to_minutes(text)Returnshours × 60 + minutes, e.g.
"09:35" → 575Raises ValueError
time must look like HH:MM, got '<text>' when there isn't exactly one : or either side isn't a whole number; hour must be 0-23, got <h>; minute must be 0-59, got <m>Ordercheck the shape first, then the hour, then the minute
Examples
callto_minutes('09:35')returns575
callto_minutes('17:00')returns1020
callto_minutes('24:00')raisesValueError