Division When Non-Zero - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second number is not zero. If the second number is zero, the program will display the message: "Division by zero is not possible". This task helps you practice conditional logic, working with user input, and handling potential errors in Python. By completing this exercise, you will strengthen your understanding of Python syntax and conditional statements like if-else. You will also gain experience in implementing safeguards for invalid operations, such as division by zero. These skills are critical for writing reliable and user-friendly Python programs that handle different input scenarios effectively.

 Category

Mastering Flow Control

 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

 Copy 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

 More Python Programming Exercises of Mastering Flow Control

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Conditional Division (Using Else)

    In this exercise, you will develop a Python program that prompts the user for two numbers. The program will display their division if the second numb...

  •  Largest Among Three Values

    In this exercise, you will develop a Python program that prompts the user to input three numbers and then determines and displays the largest one. This ...

  •  Loop Until Zero

    In this exercise, you will develop a Python program that prompts the user for a number "x" and calculates the result of 10 * x. The program will display...

  •  Loop Until Zero (Using While)

    In this exercise, you will develop a Python program that prompts the user for a number "x" and displays 10*x. This exercise is perfect for practicing user inpu...

  •  Loop with Counter

    In this exercise, you will develop a Python program that displays the numbers from 1 to 10 on the screen using a while loop. This task emphasizes the us...

  •  Multiplication Chart (Using While)

    In this exercise, you will develop a Python program that prompts the user to input a number and then displays its multiplication table using a while ...