Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Last Updated on April 18, 2023 by Prepbytes

Menu Driven Programs are designed to increase the level of user interaction within the program. Such programs receive user input and provide an output based on the selected input. In this article, we will be exploring Menu Driven Programs implemented in Python.

A menu-driven program in Python is a program that operates through a menu presented to the user, allowing them to choose from a list of options. These menus are commonly seen in devices that have microprocessors, such as washing machines and ATMs.

The usage of a menu-driven system provides two advantages. Firstly, input is reduced to a single keystroke, which decreases the likelihood of mistakes. Secondly, the system restricts the input characters, making them more clear and unambiguous. As a result, it results in an intuitive and user-friendly interface.

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")
```

Input:

``4``

Output:

``Even``

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")
```

Input:

``-3``

Output:

``Negative``

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.

Example of 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.

Code for 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()
```

Output:

``````Welcome to PrepBytes University Portal!!!
Select one of the following streams in which you want to study
1. Computer Science
2. Information Technology
3. Electronics and Communication
4. Electrical and Electronics
5. Mechanical
10. Exit
Enter the choice number from above
2
You have chosen Information Technology.
You will get the following subjects
DSA
DBMS
Computer Networks (CN)
MPMC (Multi processor and Multi Controller)
Information Security
Are you sure you want to lock this stream? Choose either A or B.
A. Yes, I'm sure
B. No, I'm not sure
A
Congratulations!! You are now an IT student at PrepBytes``````

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

Conclusion
In this article, we learned about the menu driven program in Python. We have discussed the conditional statements in python as they are necessary for the complete understanding of the menu driven program in Python. And after that we discussed the menu driven program for the PrepBytes University example. 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.