num = int(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.")
#source code --> clcoding.com
Step-by-Step Explanation
Taking Input from the User
num = int(input("Enter a number: "))
The program asks the user to enter a number using input().
The input() function takes input as a string, so we use int() to convert it into an integer.
Now, the number is stored in the variable num.
Example Inputs & Stored Values:
If the user enters 5, then num = 5
If the user enters -3, then num = -3
If the user enters 0, then num = 0
Checking the Number using if Statement
if num > 0:
print("The number is positive.")
The if statement checks if num is greater than 0.
If true, it prints:
The number is positive.
Otherwise, it moves to the next condition.
Example:
If the user enters 8, num > 0 is True, so the program prints:
The number is positive.
Checking the Number using elif Statement
elif num < 0:
print("The number is negative.")
If the first condition (if num > 0) is False, Python checks this condition.
The elif statement checks if num is less than 0.
If true, it prints:
The number is negative.
Example:
If the user enters -5, num < 0 is True, so the program prints:
The number is negative.
Handling the Case where the Number is Zero (else Statement)
else:
print("The number is zero.")
If num is not greater than 0 and not less than 0, that means it must be zero.
So, the program prints:
The number is zero.
0 Comments:
Post a Comment