Menu Driven Program in Python

In this article, we will learn how to write a menu driven program in Python. So, let’s get started by learning the conditional statements first.

Understanding the Conditional Statements in Python

Conditional statements are statements that are used to impose some conditions on code execution. Basically, a python program runs from top to bottom and left to write (in the order in which the code is written). However, sometimes it might happen that we want to run a piece of code only if a certain condition is met. This is where conditional statements come into the picture. Consider the following example given below.

Even-Odd – If Else Program in Python

num = int(input())
if num%2==0:
    print("Even")
else:
    print("Odd")

Explanation: The code above determines whether a number is an even number or an odd number. We take input from the user and if the number is divisible by 2 i.e. on dividing by 2 it leaves the remainder 0 (or number % 2 =0), then this number is even. Otherwise, the number will be odd. So, we take the input and apply the conditional statements “if-else” as shown. The piece of code written under the if suite only executes if the condition (number % 2 == 0) is true, else the condition in the “else” suite will execute.

We can extend this concept to multiple conditions as well. For instance, let us say that we want to determine whether a number is positive, negative, or 0. So, we can do it as shown in the code below.

Positive, Negative, or Zero – If Elif Else

n = int(input())
if n < 0:
    print("Negative")
elif n==0:
    print("The number is 0")
else:
    print("Positive")

Explanation: We have taken a number as input and we have applied conditions. The code under the “if” suite will execute only if the number is negative i.e. number < 0 and the code under “elif” suite will only execute if the number is 0 and the code under the “else” suite executes in all other conditions. Since there is only one possible condition other than the 2 mentioned above i.e. a number can only be positive if it is not negative or 0, so, we can say that the code under the “else” suite executes if the number is positive.

So, this is the if-elif-else. In Python, we don’t have switch-case statements. So, a Menu driven program in Python can only be created using the if-elif-else only. So, let us now create a Menu driven program in Python.

Menu-driven Program in Python

Consider the following situation. We at PrepBytes, have a university called PrepBytes University. So, we are creating a Menu driven program in Python for students who are willing to take admitted to our University. They will see 5 options. These options are the branches available in our University. They can select one among the 5 branches of Engineering. Once they select a branch from the menu, they will be displayed the subjects taught in that branch and they will have to confirm their selection. If they confirm, they will be enrolled in the University, else, they will again get a chance to select from the available branches. The menu will be provided to them until they either select a branch and confirm it, or they exit the program by entering the choice to exit the program.

The Menu driven program in Python to implement the above logic is shown below.

Menu driven Program in Python – PrepBytes University

def cse():
    print("You will get the following subjects")
    print("DSA")
    print("DBMS")
    print("Computer Networks (CN)")
def it():
    print("You will get the following subjects")
    print("DSA")
    print("DBMS")
    print("Computer Networks (CN)")
    print("MPMC (Multi processor and Multi Controller)")
    print("Information Security")
def ece():
    print("You will get the following subjects")
    print("Wireless Communication")
    print("Switching Theory and Logic Design (STLD)")
    print("Mobile Systems Communication")
def ee():
    print("You will get the following subjects")
    print("Switching Theory and Logic Design (STLD)")
    print("Control Systems")
    print("Electrical Circuits and Designs")
def mech():
    print("You will get the following subjects")
    print("Force and Friction")
    print("Motors and its Types")
def main():
    print("Welcome to PrepBytes University Portal!!!")
    print("Select one of the following streams in which you want to study")
    while(True):
        print("1. Computer Science")
        print("2. Information Technology")
        print("3. Electronics and Communication")
        print("4. Electrical and Electronics")
        print("5. Mechanical")
        print("10. Exit")
        print("Enter the choice number from above")
        choice = int(input())
        if choice == 1:
            print("You have chosen Computer Science.");
            cse();
            print("Are you sure you want to lock this stream? Choose either A or B.")
            print("A. Yes, I'm sure")
            print("B. No, I'm not sure")
            ch = input()[0]
            if ch == 'A':
                print("Congratulations!! You are now a CSE student at PrepBytes");
                break
        elif choice == 2:
            print("You have chosen Information Technology.");
            it();
            print("Are you sure you want to lock this stream? Choose either A or B.")
            print("A. Yes, I'm sure")
            print("B. No, I'm not sure")
            ch = input()[0]
            if ch == 'A':
                print("Congratulations!! You are now an IT student at PrepBytes");
                break
        elif choice == 3:
            print("You have chosen Electronics and Communication.");
            ece();
            print("Are you sure you want to lock this stream? Choose either A or B.")
            print("A. Yes, I'm sure")
            print("B. No, I'm not sure")
            ch = input()[0]
            if ch == 'A':
                print("Congratulations!! You are now an ECE student at PrepBytes");
                break
        elif choice == 4:
            print("You have chosen Electrical and Electronics.");
            ee();
            print("Are you sure you want to lock this stream? Choose either A or B.")
            print("A. Yes, I'm sure")
            print("B. No, I'm not sure")
            ch = input()[0]
            if ch == 'A':
                print("Congratulations!! You are now an EE student at PrepBytes");
                break
        elif choice == 5:
            print("You have chosen Mechanical Engineering.");
            mech();
            print("Are you sure you want to lock this stream? Choose either A or B.")
            print("A. Yes, I'm sure")
            print("B. No, I'm not sure")
            ch = input()[0]
            if ch == 'A':
                print("Congratulations!! You are now a ME student at PrepBytes");
                break
        elif choice == 10:
            break
        else:
            print("You have entered an invalid choice.")

if __name__ == '__main__':
    main()

So, the above program is one of the simplest and most extensive examples of a menu driven program in Python.

With this, we come to the end of our discussion in this article. We hope that you liked the discussion and hope to see you again soon at PrepBytes.

Other Python Programs
Python program to reverse a number
Python program for heap sort
Python program to check armstrong number
Python program to check leap year
Python program to convert celsius to fahrenheit
Python program to find factorial of a number
Python program to reverse a linked list
Python Program to find the middle of a linked list using only one traversal
Python Program to Add Two Numbers
Python Program to Check Palindrome Number
Python Program to Print the Fibonacci Series

Leave a Reply

Your email address will not be published. Required fields are marked *