Introduction to Python

Activity 2: Eligible to Vote

Lesson 3: Algorithms

In this activity, we will write a Python program that checks whether a person is eligible to vote based on their age. In most countries, the minimum voting age is 18.

Illustration of a ballot box with a checkmark and a person next to an age 18 sign representing voting eligibility

Algorithm

  • START
  • INPUT: Ask the user for their name
  • INPUT: Ask the user for their age
  • DECISION: Is age >= 18?
  • YES → OUTPUT: Print that the person is eligible to vote
  • NO → OUTPUT: Print that the person is not eligible and how many years remain
  • END
Flowchart for the voting eligibility checker showing start, input boxes for name and age, a diamond decision for age >= 18, a yes branch to eligible message and a no branch to not eligible message with years remaining, then end

Writing the Python Code

# Voting Eligibility Checker name = input("Enter your name: ") age = int(input("Enter your age: ")) if age >= 18: print(name + ", you are eligible to vote!") else: years_left = 18 - age print(name + ", you are not eligible to vote yet.") print("You need to wait " + str(years_left) + " more year(s).")

Understanding the Code

Code Breakdown

LineWhat it Does
name = input(...)Stores the user's name as a string
age = int(input(...))Stores the user's age as an integer
if age >= 18Checks if the age is 18 or more
years_left = 18 - ageCalculates how many years until the person can vote
str(years_left)Converts the number to a string for printing

Testing the Program

  • Run with name = Sara, age = 20 → Should print Sara, you are eligible to vote!
  • Run with name = Ali, age = 15 → Should print Ali, you are not eligible to vote yet. You need to wait 3 more year(s).
Python console showing two test runs — one where Sara aged 20 is eligible and one where Ali aged 15 is not eligible with years remaining shown
Save your file with the name Voting Eligibility Checker before moving on!

Subscribe to our newsletter.

Get updates to news and events.