Codington
0 of 6 problems solved • 0%

Exercise prompt

Validate a Share

A caller may prefer an explicit failure when its inputs are invalid. Use raise ValueError(message) to explain a rejected value.

Example

A function can reject an argument before using it. Here the caller catches the exception and displays its message.

def label(gate): if gate == "": raise ValueError("gate is missing") return "Gate " + gate try: label("") except ValueError as error: print(error) # gate is missing

Raising ends the function immediately. The caller decides how to present the problem.

Your task

Implement share(total, people) for a nonnegative amount and an integer head count. Return the amount per person for positive head counts. For zero or a negative count, raise ValueError with the exact message people must be positive.

share(9, 2) # 4.5 share(9, 0) # raises ValueError("people must be positive")