Python tutorial to check if a user is eligible for voting or not :
In this python tutorial, we will learn how to check if a user is eligible for voting or not. The program will take the age as an input from the user, check the eligibility and then print out the result.
This program will give you the basic understanding of ternary if-else condition and how to read user input values. Below the program we will explain you how it actually works. Let’s take a look at the program first :
Python program :
age = int(input("Enter your age : ")) print("You are eligible for voting" if age > 18 else "You are not eligible for voting")
Enter your age : 13 You are not eligible for voting Enter your age : 19 You are eligible for voting
As you can see that our program is only of two lines. So,let’s take a look at the steps what we have done in this program :
1. First of all, we are reading the input from the user using input() method. This method returns a string, so we are using int(input()) to convert the input to an integer.
2. Then we are checking if the value is greater than 18 or not. This is known as ternary conditional operator in python. If you are familier with C language, then you may have seen condition ? statement : statement operator. This is simililar to the one we have used in this program.
So, it will check if the user is eligible or not for voting and print out the result.
This program has taught you how to use ternary conditional operator in python and how to read a user input number. Try to run the program on your side and drop a comment below if you have any queries.
You might also like :
- Python While and for loop
- Python program to print a right angle triangle
- Python issubclass function in details
- Python Tuple
- Python Set
- Python Break , continue and pass