Successful conversion and errors inside else

A Python 3 ticket system calls the following function independently for each input string. Complete the outcome table with the exact integer returned, the word invalid, or the word propagates if an exception leaves the function without being caught.


For this question, converting "bad" with int raises ValueError, and integer division by zero raises ZeroDivisionError. Quotation marks in the input column identify strings; do not include quotation marks in your answers.


Python
def ticket_count(text):
    try:
        count = int(text)
    except (ValueError, ZeroDivisionError):
        return "invalid"
    else:
        if count == 0:
            return 24 // count
        return count + 2
Input textOutcome
"5"
"bad"
"0"
"-2"

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More try/except Blocks Questions: