Multiply Unless Zero - Python Programming Exercise

In this exercise, you will develop a Python program that prompts the user for a number. If the number is not zero, the program will ask for a second number and display their sum. If the first number is zero, the program will simply display "0". This task helps you practice conditional logic and working with user input in Python. By completing this exercise, you will enhance your understanding of Python syntax and improve your ability to use if-else statements effectively. You will also gain experience handling multiple user inputs and performing basic arithmetic operations. These skills are fundamental for creating interactive Python programs that adapt based on user actions.

 Category

Mastering Flow Control

 Exercise

Multiply Unless Zero

 Objective

Develop a Python program that prompts the user for a number. If the number is not zero, it will ask for a second number and display their sum; otherwise, it will display "0".

 Example Python Exercise

 Copy Python Code
# Prompt the user to enter the first number
num1 = float(input("Please enter a number: "))

# Check if the first number is not zero
if num1 != 0:
    # Prompt the user to enter the second number
    num2 = float(input("Please enter another number: "))
    # Calculate and display the sum of the two numbers
    print(f"The sum of {num1} and {num2} is {num1 + num2}")
else:
    # Display "0" if the first number is zero
    print("0")

 Output

Please enter a number: 5
Please enter another number: 3
The sum of 5.0 and 3.0 is 8.0

 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.

  •  Division When Non-Zero

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

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