Practice questions for conditional statements



Introduction

The conditional statements in programming languages decide the direction of the flow of program execution. It is used for decision-making. In Python, there is no switch or case statement like in other programming languages to make decisions in the program. However, it has if, elif, and else statement to make a decision.

The decision is made based on the condition provided to the if and elif statement(conditional statements). However, if none of the conditions gets fulfilled, else part will be executed.

All the practice questions included here consist of source code and it’s organized from a top-down level. First, you will practice on the if-else statement, then the elif statement. We go further deeper, you will practice nested if and elif statements including multiple conditions.

In case, if you need a full PowerPoint presentation on the conditional statements in python click here.

Before you start attempting the practice questions below, make sure you already have a concept on the following topics.



Question 1: if-else statement
Eligibility to vote

Write a python program to check whether you are eligible to vote or not?
Your program should get the age of the voter from the user and if their age is 18 and above, let them vote otherwise deny them from voting.

Source code:

age = int(input("Enter voters age:"))
if age >= 18:
    print("You can cast your vote!")
else:
    print("Sorry! You are not eligible to vote!")

Output:

#output 1
Enter voters age:17
Sorry! You are not eligible to vote!

# output 2
Enter voters age:20
You can cast your vote!


Question 2: elif statement
1. Traffic light

Write a python program that will check for the following conditions:

  • If the light is green – Car is allowed to go
  • If the light is yellow – Car has to wait
  • If the light is red – Car has to stop
  • Other signal – unrecognized signal. Example black, blue, etc…

Source code:

signal = input("What is a traffic signal? :").title()
if signal == "Red":
    print("Stop your car!")
elif signal == "Yellow":
    print("Wait your car!")
elif signal == "Green":
    print("You are allowed to go!")
else:
    print("Unrecgonized signal!")

Output:

#Output 1
What is a traffic signal? :red
Stop your car!

#Output 2
What is a traffic signal? :gReen
You are allowed to go!

#Output 3
What is a traffic signal? :Black
Unrecgonized signal!

#Output 4
What is a traffic signal? :yellow
Wait your car!
2. Students grade

Write a program to check students’ grades.  Your program should fulfill the following conditions:

  1. Grade A – Outstanding
  2. Grade B – Excellent
  3. Grade C – Very Good
  4. Grade D – Good
  5. Grade E – Satisfactory
  6. others – Unrecognized

A program should also ask to enter the student’s name, class, and section. The expected output is attached below.

students grade

Source code:

name = input("Enter student name:")
clas = input("Enter class:")
section = input("Enter section:")
grade = input("Enter students grade:").upper()
print("--------------------")
print("Name:", name)
print("Class:", clas)
print("Section:", section)
if grade == "A":
    print("Grade: Outstanding!")
elif grade == "B":
    print("Grade: Excellent!")
elif grade == "C":
    print("Grade: Very Good!")
elif grade == "D":
    print("Grade: Good!")
elif grade == "E":
    print("Grade: Satisfactory!")
else:
    print("Unrecognized Grade!")

Output:

Enter student name:Sonam
Enter class:11
Enter section:D
Enter students grade:B
--------------------
Name: Sonam
Class: 11
Section: D
Grade: Excellent!


Question 3: Multiple conditions
Students result with grade

Modify the earlier program students’ grades in such a way that they should take in five subject marks. Find the total mark and their percentage. Your program should check for the following conditions:

  • If the percentage falls below 45, they are considered fail.
  • If the percentage is between 45 and 60, grade them as pass.
  • If the percentage is between 60 and 75, grade them as good.
  • If the percentage is between 75 and 85, grade them as very good.
  • If the percentage is between 85 and 100, grade them excellent.
  • If the percentage is below zero or above 100, it’s an error.


The expected output is attached below.

student result with grade

Source code:

name = input("Enter name:").title()
clas = input("Enter class:")
section = input("Enter section:")
eng = float(input("Enter English mark:"))
dzo = float(input("Enter Dzongkha mark:"))
math = float(input("Enter Math mark:"))
his = float(input("Enter History mark:"))
geo = float(input("Enter Geography mark:"))

total_mark = eng + dzo + math + his + geo
percentage = total_mark / 5

print("\n---------Printing result-------------")
print("Name:", name)
print("Class:", clas)
print("Section:", section)
print("Percentage:", percentage,"%")
if percentage < 0 or percentage > 100:
    print("Error: percentage should be between 0 and 100 only!")
elif percentage < 45:
    print("Failed!")
elif percentage >= 45:
    print("Pass!")
    if percentage >=45 and percentage < 60:
        print("Remark: Just passed!")
    elif percentage >= 60 and percentage < 75:
        print("Remark: Good!")
    elif percentage >= 75 and percentage < 85:
        print("Remark: Very Good!")
    elif percentage >= 85 and percentage < 100:
        print("Remark: Excellent!")

Output:

Enter name:Sonam Dorji
Enter class:12
Enter section:E
Enter English mark:67
Enter Dzongkha mark:89
Enter Math mark:90
Enter History mark:56
Enter Geography mark:88

---------Printing result-------------
Name: Sonam Dorji
Class: 12
Section: E
Percentage: 78.0 %
Pass!
Remark: Very Good!


Question 4: Nested condition
Trace your subject mark

Write a program to trace your subject mark. Your program should fulfill the following conditions:

  1. If the subject mark is below 0 and above 100, print “error: mark should be between 0 and 100 only”
  2. Students will fail in the subject if their mark is below 50.
  3. Students will pass in the subject if they score 50 and above.
    1. If subject mark is between 50 and 60, grade student as good.
    2. If subject mark is between 60 and 80, grade student as very good.
    3. If subject mark is between 80 and 100, grade student as outstanding.

Make sure to print their mark in every statement to prove that the condition is fulfilled. Moreover, name, class, and section should be also displayed along with the marks and their grade.

The expected output is here attached below.

Subject mark

Source code:

name = input("Enter name:").title()
clas = input("Enter class:")
section = input("Enter section:")
subject = input("Enter the subject name:")
mark = float(input("Mark scored in that subject:"))

print("\n----Tracing your", subject, "mark------")
print("Name:", name)
print("Class:", clas)
print("Section:", section)
if mark < 0 or mark > 100:
    print("Error: Mark should be between 0 and 100 only!")
elif mark < 50:
    print(subject, "mark is ", mark)
    print("Failed in ", subject)
elif mark >= 50:
    print(subject, "mark is ", mark)
    print("Congratulations! Pass in ", subject)
    if mark >=50 and mark < 60:
        print("Remark: Good in ", subject)
    elif mark >= 60 and mark < 80:
        print("Remark: Very good in ", subject)
    elif mark >= 80 and mark <= 100:
        print("Remark: Outstanding in ", subject)

Output:

Enter name:Dawa
Enter class:12
Enter section:A
Enter the subject name:Dzongkha
Mark scored in that subject:93

----Tracing your Dzongkha mark------
Name: Dawa
Class: 12
Section: A
Dzongkha mark is  93.0
Congratulations! Pass in  Dzongkha
Remark: Outstanding in  Dzongkha