Exercise
Division When Non-Zero
Objective
Develop a Python program that prompts the user for two numbers and displays their division if the second number is not zero; otherwise, it will display "Division by zero is not possible".
Example Python Exercise
Show Python Code
# Prompt the user to enter the first number
num1 = float(input("Please enter the first number: "))
# Prompt the user to enter the second number
num2 = float(input("Please enter the second number: "))
# Check if the second number is not zero
if num2 != 0:
# Calculate and display the division
print(f"The result of dividing {num1} by {num2} is {num1 / num2}")
else:
# Display a message if the second number is zero
print("Division by zero is not possible")
Output
Case 1:
Please enter the first number: 10
Please enter the second number: 2
The result of dividing 10.0 by 2.0 is 5.0
Case 2:
Please enter the first number: 10
Please enter the second number: 0
Division by zero is not possible
Share this Python Exercise