3-1-5-15. Solution: Handling Input Errors

Here is our solution for what the revised party_planner function on the previous page could look like, using a try-except block:

def party_planner(cookies, people):
    leftovers = None
    num_each = None

    try:
        num_each = cookies // people
        leftovers = cookies % people
    except ZeroDivisionError:
        print("Oops, you entered 0 people will be attending.")
        print("Please enter a good number of people for a party.")

    return(num_each, leftovers)
%d 블로거가 이것을 좋아합니다: