Skip to content

Latest commit

 

History

History
41 lines (37 loc) · 1.54 KB

File metadata and controls

41 lines (37 loc) · 1.54 KB
def check_number_type(num):
    if isinstance(num, complex):
        if num.real == 0 and num.imag == 0:
            return "Neutral (Zero)"
        else:
            return "Neither positive nor negative (complex number)"
    else:
        if num > 0:
            return "Positive"
        elif num < 0:
            return "Negative"
        else:
            return "Neutral (Zero)"

# Input from the user
user_input = input("Enter a number (real or complex): ")

# Try to convert the input into a complex number first
try:
    number = complex(user_input)
except ValueError:
    print("Invalid input. Please enter a valid number.")
else:
    result = check_number_type(number)
    print(f"The number you entered is: {result}")

How It Works:

  1. Input Handling: The program takes input from the user and tries to convert it into a complex number.
  2. Type Checking: The program checks if the number is of type complex using isinstance().
    • For complex numbers:
      • If both the real and imaginary parts are zero, it is considered "Neutral (Zero)".
      • If not, the program outputs that the number is neither positive nor negative, as these concepts don't directly apply to complex numbers.
    • For real numbers:
      • Positive if the number is greater than zero.
      • Negative if the number is less than zero.
      • Neutral if the number is exactly zero.
  3. Output: The program prints whether the number is positive, negative, or neutral.

This approach ensures that the program correctly handles both real and complex numbers.