Menu-Driven Programs in Python

Menu-Driven Programs in Python

An Introduction to Menu-Driven Program

Menu-Driven Program is a program that gets input from a user by showing the options list, known as the menu, from which the user chooses their option. Systems processing the Menu-Driven programs are ordinary, starting from washing machines controlled by Microprocessors to Automated Teller Machines (ATMs). Taking the ATM case, the user presses single keys to indicate the type of transaction (if the user wants a receipt with the cash, or if an account statement is needed). With many, the user presses a single key to indicate the amount of cash for withdrawal.

The Menu-Driven Systems are beneficial in two ways: At first, input is taken by the single keystrokes, which reduces the chance of the system too prone to user error. Secondly, Menu-Driven Systems limits the characters range resulting in the way where the entered input becomes unambiguous. Hence, these two characteristics make the whole system pretty user-friendly.

In the following tutorial, we will discover some Menu-Driven Programs written in Python. These programs will let us understand different aspects of Menu-Driven Programs along with different libraries and modules of Python Programming Language.

So, let’s get started.

Calculate the Parameter and Area of different Shapes using functions

Program:

Output:

WELCOME TO A SIMPLE MENSURATION PROGRAM

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:1

CALCULATE PARAMETER
1. Circle
2. Rectangle
3. Square
4. Exit
Enter the Choice:2
Enter Height of Rectangle:4
Enter Width of Rectangle:5
Parameter of Rectangle: 18

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:2

CALCULATE AREA
1. Circle
2. Rectangle
3. Square
4. Exit
Enter the Choice:1
Enter Radius of Circle:2
Area of Circle: 12.56

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:5
Oops! Incorrect Choice.

MAIN MENU
1. Calculate Parameter
2. Calculate Area
3. Exit
Enter the Choice:3

Menu-Driven Programs in Python

Explanation:

In the above example, we have defined different functions printing the estimated value after calculation. These functions include the parameters and areas of circle, rectangle, and square, respectively. We have then printed the heading of the program saying: WELCOME TO A SIMPLE MENSURATION PROGRAM. Below that, we have used the infinite while loop to print the Main Menu containing different options. The program then uses the if-elif-else statements to ask the user to input the integer choosing the options. The program will also raise an exception if the inserted integer is not present in the options list. We have then created two different submenus separating the Parameter option and the Area option. We have then added few more options within these submenus describing different shapes. These options also take different integer values indicating the radius for circle, height and width for rectangle, and side for square. As a result, the menu-driven program is successfully created and is able to calculate the parameter and areas of different shapes.

Menu-Driven Program to create a simple calculator

In the following Menu-Driven Program, we are going to build a simple calculator in Python. We will use the infinite while loop and functions same as above. We will design a menu allowing the user to interact with the calculator functions such as addition, subtract, multiplication and division.

Let us consider the following program’s syntax:

Program:

Output:

WELCOME TO A SIMPLE CALCULATOR

MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit

Enter the Choice: 1

ADDITION

First Number: 3
Second Number: 4
3 + 4 = 7

MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit

Enter the Choice: 2

SUBTRACTION

First Number: 6
Second Number: 3
6 - 3 = 3

MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit

Enter the Choice: 3

MULTIPLICATION

First Number: 8
Second Number: 2
8 x 2 = 16

MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit

Enter the Choice: 4

DIVISION

First Number: 10
Second Number: 4
10 / 4 = 2.5

MENU
1. Sum of two Numbers
2. Difference between two Numbers
3. Product of two Numbers
4. Division of two Numbers
5. Exit

Enter the Choice: 5

Menu-Driven Programs in Python

Explanation:

In the above program, we have used almost similar procedure we did in the previous program. We have defined various functions such as add, subtract, multiply, and divide. We have then used the while loop to print the menu list to the users and if-elif-else statements to return the answers that the user needed. As a result, a simple calculator is successfully created and performs some basic calculations like addition, subtraction, multiplication, and division.

Menu-Driven Program to create a Phone Directory

In the following Menu-Driven Program, we are going to create a Phonebook Directory using the different functions. We will add the following features to the Phonebook Directory:

  1. Storing the Contact Numbers of People
  2. Searching for the Contact Number using the person’s name

Let us implement this idea in the following program:

Program:

Output:

WELCOME TO THE PHONEBOOK DIRECTORY

MAIN MENU

1. Show all existing Contacts
2. Add a new Contact
3. Search the existing Contact
4. Exit
Enter your choice: 1
There is no contact in the phonebook.
Press Enter to continue ...

MAIN MENU

1. Show all existing Contacts
2. Add a new Contact
3. Search the existing Contact
4. Exit
Enter your choice: 2
Enter your First Name: Mark
Enter your Last Name: Henry
Enter your Phone number: 1234567890
Enter your E-mail Address: [email protected]
The following Contact Details:
 [Mark Henry, 1234567890, [email protected]]

has been stored successfully!
Press Enter to continue ...

MAIN MENU

1. Show all existing Contacts
2. Add a new Contact
3. Search the existing Contact
4. Exit
Enter your choice: 3
Enter First name for Searching contact record: Mark
Your Required Contact Record is: [Mark Henry, 1234567890, [email protected]]

Press Enter to continue ...

MAIN MENU

1. Show all existing Contacts
2. Add a new Contact
3. Search the existing Contact
4. Exit
Enter your choice: 1
[Mark Henry, 1234567890, [email protected]]

Press Enter to continue ...

MAIN MENU

1. Show all existing Contacts
2. Add a new Contact
3. Search the existing Contact
4. Exit
Enter your choice: 4
Thank you for using Phonebook!

Explanation:

In the above Menu-Driven Program, we have created a Phonebook Directory that can store a new contact in a text file, display the stored contacts and allow users to search an existing number too. First of all, we have created a text file to store the contact details. We have then defined various functions in order to add, show, and search different contacts. We have also created different contact detail fields such as First Name, Last Name, Mobile Number, and E-mail Address. As a result, the program is completed successfully, and the output of the same can be seen above.

Conclusion

In the above tutorial, we have understood the meaning of Menu-Driven Programming along with some examples. We have created three different programs, including the mensuration program, a simple calculator, and a phonebook directory. Apart from these three, there are many other programs that one can create.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263541.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论