What You'll Learn
- •Applying comparison operators with integers
- •Handling invalid input ranges
- •Using elif for cleaner branching
Python
# Program to check voting eligibility
age = int(input("Enter your age: "))
if age < 0:
print("Age cannot be negative.")
elif age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote yet.")Output
Enter your age: 17 You are not eligible to vote yet.
We assume a minimum voting age of 18:
- Negative ages are invalid and handled first.
- 18 or older → eligible.
- Below 18 → not yet eligible.
This is a classic example of simple threshold-based decision making.
Step-by-Step Breakdown
- 1Read age as an integer.
- 2If age is negative, show an error.
- 3Else if age is at least 18, mark eligible.
- 4Otherwise, mark not eligible.