Practical 3: Program to Determine Whether a Person is Eligible to Vote or Not
Problem Statement
Write a Python program to determine whether a person is eligible to vote or not based on their age. The eligibility criteria are as follows:
- A person is eligible to vote if their age is 18 years or more.
- The program should ask for the user’s age and print a message indicating whether they are eligible to vote.
Solution Code
# Program to check voting eligibility
# Input: Take age of the person from the user
age = int(input("Enter your age: "))
# Check if the person is eligible to vote
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Output
- The program will prompt the user to input their age.
- It will then print a message indicating whether the user is eligible to vote or not.
Example outputs for different inputs:
For age above or equal to 18:
Enter your age: 18
You are eligible to vote.
Process finished with exit code 0
For age below 18:
Enter your age: 15
You are not eligible to vote.
Process finished with exit code 0
Explanation
Below is the explanation of F.E. PPS Unit 2 Practical 3 solution where we have written Python program to determine whether a person is eligible to vote or not.
- Input of Age:
- The program asks the user to input their age using the input() function. The input is converted to an integer using the int() function.
- Eligibility Check:
- The program uses an if-else statement to check if the age is greater than or equal to 18.
- If the age is 18 or greater, it prints: “You are eligible to vote.”
- Otherwise, it prints: “You are not eligible to vote.”
- The program uses an if-else statement to check if the age is greater than or equal to 18.
- Displaying the Result:
- Based on the result of the condition, the program prints the appropriate message indicating the user’s voting eligibility.
Key Concept Learned
- Conditional Statements (if-else):
- This program demonstrates how to use conditional statements (if-else) in Python to make decisions based on the user’s input.
- It also shows how to compare values (in this case, the user’s age) to determine the outcome.
- Input and Type Conversion:
- The program also illustrates how to take user input and convert it to the correct data type (int in this case) for comparison.
- Decision Making in Python:
- By using an if condition, the program is able to branch and execute different code based on the age input.