Python Program to Check if a Person Is Eligible to Vote
Beginner · Basics
In India, as in most countries, a citizen must be at least 18 years old to vote. This program checks whether a person is old enough and, if not, how many years they still have to wait.
Program
voting_eligibility.py
name = "Kavya" # or: name = input("Enter your name: ")
age = 16 # or: age = int(input("Enter your age: "))
VOTING_AGE = 18
if age < 0:
print("Age cannot be negative")
elif age >= VOTING_AGE:
print(f"{name}, you are {age}, so you are eligible to vote.")
else:
years_left = VOTING_AGE - age
print(f"{name}, you are {age}, so you are not eligible to vote yet.")
print(f"You can vote in {years_left} year(s).")
Output
Kavya, you are 16, so you are not eligible to vote yet. You can vote in 2 year(s).
How it works
- The voting age is stored in
VOTING_AGE, so it is easy to change for a country with a different rule. - A negative age is impossible, so it is caught first.
age >= VOTING_AGEis true at exactly 18 as well as above it, which is why>=is used rather than>.- For someone younger,
VOTING_AGE - agegives the number of years left to wait.
Other ways to write it
From a date of birth
From a date of birth
from datetime import date
birth_date = date(2008, 11, 25)
on_date = date(2026, 1, 15) # or: date.today()
# Take 1 off if this year's birthday has not come yet
age = on_date.year - birth_date.year
if (on_date.month, on_date.day) < (birth_date.month, birth_date.day):
age -= 1
print("Age on", on_date, "is", age)
print("Eligible to vote" if age >= 18 else "Not eligible to vote yet")
Output
Age on 2026-01-15 is 17 Not eligible to vote yet
Subtracting the years alone would say 18, but the birthday in November has not come yet in January, so the real age is 17.
Checking a list of people
Checking a list of people
people = [("Arjun", 19), ("Meera", 17), ("Rahul", 18), ("Sana", 45)]
for name, age in people:
status = "can vote" if age >= 18 else "cannot vote yet"
print(f"{name} ({age}): {status}")
voters = [name for name, age in people if age >= 18]
print("Eligible voters:", ", ".join(voters))
Output
Arjun (19): can vote Meera (17): cannot vote yet Rahul (18): can vote Sana (45): can vote Eligible voters: Arjun, Rahul, Sana
Each person is a (name, age) tuple, and the loop unpacks it into two variables.
Try it yourself
Add a check for the minimum age to stand for election to the Lok Sabha, which is 25.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises