w3resource

Python Program: Voting eligibility checker


3. Voting Eligibility Checker

Write a program that calculates whether a person is eligible for voting based on their age using boolean conditions.

Sample Solution:

Code:

def voter_eligibility(age):
    return age >= 18

def main():
    try:
        age = int(input("Input your age: "))
        
        if age < 0:
            print("Age cannot be negative.")
        elif voter_eligibility(age):
            print("You are eligible to vote.")
        else:
            print("You are not eligible to vote.")
    except ValueError:
        print("Invalid input. Please input a valid age.")

if __name__ == "__main__":
    main()

Output:

Input your age: 17
You are not eligible to vote.
Input your age: -14
Age cannot be negative.
Input your age: 18
You are eligible to vote.

When we run the above exercise, it prompts the user to enter their age. Based on the person's age, the "voter_eligibility()" function determines whether they are eligible to vote and prints the appropriate message.

Flowchart:

Flowchart: Voting eligibility checker.

For more Practice: Solve these Related Problems:

  • Write a Python program that checks if a person is eligible to vote by evaluating if their age is 18 or older using boolean conditions.
  • Write a Python function to determine voting eligibility by verifying that the age is at least 18 and that the user is registered, using boolean logic.
  • Write a Python script to prompt for a person’s age and citizenship status, then use boolean expressions to decide and print if they can vote.
  • Write a Python program to check voting eligibility where both age and a valid voter ID are required, using boolean conditions to return True if eligible.

Go to:


Previous: Python logical AND and OR operations with Boolean values.
Next: Python list empty check using Boolean logics.

Python Code Editor :

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.