Python_Institute
PCEP-30-02 · Question #233
What is the output of the following program if the user enters kangaroo at the first prompt and 0 at the second prompt? ``python try: first_prompt = input("Enter the first value: ") a =…
User enters 'kangaroo' for the first prompt. first_prompt becomes 'kangaroo'. a = len('kangaroo') becomes 8. User enters '0' for the second prompt. second_prompt becomes '0'. b = len('0') becomes 1. The program then executes print(a/b), which is print(8/1). This evaluates to…
Module 3: Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise Operations
Question
What is the output of the following program if the user enters kangaroo at the first prompt and 0 at the second prompt?
try:
first_prompt = input("Enter the first value: ")
a = len(first_prompt)
second_prompt = input("Enter the second value: ")
b = len(second_prompt)
print(a/b)
except ZeroDivisionError:
print("Do not divide by zero!")
except ValueError:
print("Wrong value.")
except:
print("Error.Error.Error.")
Explanation
User enters 'kangaroo' for the first prompt. first_prompt becomes 'kangaroo'. a = len('kangaroo') becomes 8.
User enters '0' for the second prompt. second_prompt becomes '0'. b = len('0') becomes 1.
The program then executes print(a/b), which is print(8/1). This evaluates to 8.0.
No exceptions are raised, so the try block completes successfully.
Output: 8.0
Topics
#try/except#exception handling#len function#ZeroDivisionError
Community Discussion
No community discussion yet for this question.