Conditional Division (Using Else) - 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, it will display the message: "Division by zero is not possible". This task emphasizes the importance of handling user input and using conditional logic to manage potential errors in Python. By completing this exercise, you will improve your understanding of Python syntax and learn how to create more robust Python programs. You will also practice implementing error-handling measures, ensuring your program behaves predictably when encountering division by zero. These foundational skills are crucial for developing reliable and user-friendly Python applications.

 Category

Mastering Flow Control

 Exercise

Conditional Division (Using Else)

 Objective

Develop a Python program that prompts the user for two numbers. If the second number is not zero, it will display their division; 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.

  •  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 ...

  •  Descending Odd Values

    In this exercise, you will develop a Python program that displays the odd numbers from 15 to 7 in descending order using a while loop. This task ...