Single or Pair of Negative Values - Python Programming Exercise

In this exercise, you will 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. This task will help you practice using conditional statements and logical operators in Python to handle multiple conditions and user inputs effectively. By completing this exercise, you will enhance your skills in making decisions based on multiple conditions and user input. Understanding how to evaluate whether one or both values meet a particular condition is fundamental in building Python programs that require complex decision-making and data validation logic.

 Category

Mastering Flow Control

 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

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

 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.

  •  Factors and Multiples

    In this exercise, you will develop a Python program that displays the numbers from 1 to 500 that are multiples of both 3 and 5 on the screen. You...

  •  Repeated Value

    In this exercise, you will develop a Python program that prompts the user for a number and a quantity, then displays that number repeated as many...

  •  Access Code

    In this exercise, you will develop a Python program that prompts the user to enter their login and password (both must be integer numbers). The p...

  •  Access Code V2

    In this exercise, you will develop a Python program that prompts the user for their login and password, both of which must be integers. The progr...

  •  Multiple Divisions

    In this exercise, you will develop a Python program that prompts the user for two numbers and displays their division and remainder. If 0 is entered as ...

  •  Multiple Times Tables (Using While)

    In this exercise, you will develop a Python program that displays multiplication tables from 2 to 6 using nested "while" loops. The program will iterate throug...