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