Python | Input age and check eligibility for voting

Python | if else example: Here, we are implementing a program, it will read age from the user and check whether person is eligible for voting or not.
By Pankaj Singh Last updated : January 05, 2024

Problem statement

Input age of the person and check whether a person is eligible for voting or not in Python.

Input age and check eligibility for voting

This is a simple if else example in the python - Here, we will read the age of the person by using input() function and convert the entered age value to the integer by using int() function. Then we will check the condition, whether age is greater than or equal to 18 or not - if age is greater than or equal to 18, the person will be eligible for the voting.

Python program to input age and check eligibility for voting

# input age
age = int(input("Enter Age : "))

# condition to check voting eligibility
if age >= 18:
    status = "Eligible"
else:
    status = "Not Eligible"

print("You are ", status, " for Vote.")

Output

The output of the above program is:

RUN 1:
Enter Age : 21
You are  Eligible  for Vote.

RUN 2:
Enter Age : 17
You are  Not Eligible  for Vote.

Check voting eligibility using user-defined function

Here, we are creating a user-defined function checkEligibility(), passing age and returning True if age is greater than or equal to 18; False, otherwise.

# Create a function to check eligibility
# for voting
def checkEligibility(a):
    # condition to check voting eligibility
    if age >= 18:
        return True
    else:
        return False

# Main code
# input age
age = int(input("Enter Age : "))

# Call function and print result
result = checkEligibility(age)

if result:
    print("Yes! You're eligible for voting")
else:
    print("No! You're not eligible for voting")

Output

The output of the above program is:

RUN 1:
Enter Age : 21
Yes! You're eligible for voting

RUN 2:
Enter Age : 17
No! You're not eligible for voting

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.