Skip to content

script_11.py - Positive, Negative, or Zero Checker

Code

Python
#!/usr/bin/env python3
"""Positive, negative, or zero checker"""

num = float(input("Enter a number: "))

if num > 0:
    print("The number is positive")
elif num < 0:
    print("The number is negative")
else:
    print("The number is zero")

Explanation

Line 4: num = float(input("Enter a number: ")) - input() returns string type (str) - float() converts to floating-point type allowing decimals and negatives - num variable stores the float value

Line 6: if num > 0: - > is greater-than comparison operator - Returns True if num is any positive value (including 0.1, 100, etc.) - Returns False for zero and negative numbers

Line 8: elif num < 0: - < is less-than comparison operator - Only checked if first condition was False (num is not positive) - Returns True for negative values (-1, -0.5, etc.)

Line 10: else: - Only executes when both previous conditions are False - num is neither positive nor negative, so it must be exactly zero - Handles the case where num == 0