In previous two articles, we discussed conditional statements such as
Note that the condition 1 comes from if-block, condition 2 comes from elif, condition 3 is elif and else block is D.
The first condition,
Example – If-then-elif-else
In this example, student receive his grade in
\begin{aligned} &A = Excellent\\ &B = Outstanding\\ &C = Very \hspace{3px}Good\\ &D = Good\\ &E = Fair \\ &F = Fail \end{aligned}
We need to print the result based on the grade that the student gets. Consider the following code.
grade = input("Enter Your Grade:" ) if grade == "A": print("You grade is Excellent!") elif grade == "B": print("Your grade is Outstanding!") elif grade == "C": print("Your grade is Very Good!") elif grade == "D": print("Your grade is Good!") elif grade == "E": print("Your grade is Fair..Work Hard!") elif grade =="F": print("Your Failed.. Try again later") else: print("Wrong Input")
The output of the program is given below.
Output – If-then-elif-else
=========== RESTART: C:/Python_projects/Display_Student_Result.py =========== Enter Your Grade:D Your grade is Good! >>>