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 :
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 familiar with C language, then you may have seen the “condition ? statement : statement ” operator. This is similar 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 the 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 :