Three possible answers need three branches: if, elif and else.
The program
python
n = float(input("Enter a number: ")) if n > 0: print("Positive") elif n < 0: print("Negative") else: print("Zero")
Typed in this example: -3.5. Press Run to type your own.
output
Enter a number: -3.5 Negative
How it works
- Python checks the conditions from top to bottom and runs only the first branch whose condition is true.
elifmeans "else if": it is only checked when theifabove it was false.elsecatches everything left. If a number is not more than 0 and not less than 0, it must be 0, so no third test is needed.float()means the program accepts decimals such as-3.5as well as whole numbers.