Exercise
Single Or Pair Of Negative Values
Objective
Develop a Python program that prompts the user for two numbers and checks whether both are negative, only one is negative, or neither is negative.
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 whether both numbers are negative, only one is negative, or neither is negative
if num1 < 0 and num2 < 0:
print(f"Both {num1} and {num2} are negative.")
elif num1 < 0 or num2 < 0:
print(f"Only one of the numbers is negative.")
else:
print(f"Neither of the numbers is negative.")
Output
Case 1:
Please enter the first number: -5 Please enter the second number: -3 Both -5.0 and -3.0 are negative.
Case 2:
Please enter the first number: -5 Please enter the second number: 3 Only one of the numbers is negative.
Case 3:
Please enter the first number: 5
Please enter the second number: 3
Neither of the numbers is negative.
Share this Python Exercise