Check voting eligibility

EasyIf-Else Programs · 15 of 64

Write a Python program to check whether a person is eligible to vote (age 18 or above).

Advertisement
Input
17, 18, 45
Output
17 -> not eligible, wait 1 year(s)
18 -> eligible to vote
45 -> eligible to vote

Approach

  1. Compare the age with 18 using >=.

Solution

Python
for age in [17, 18, 45]:    if age >= 18:        print(age, "-> eligible to vote")    else:        print(age, "-> not eligible, wait", 18 - age, "year(s)")
Output
17 -> not eligible, wait 1 year(s)
18 -> eligible to vote
45 -> eligible to vote

Line by line

Line 2

>= includes exactly 18, which is the legal voting age in India.

Line 5

The else branch also tells how long to wait.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs