Python Employee Array Menu Driven Program

Python Employee Array Menu Driven Program
Here is a sample Python menu-driven application that prompts the user to enter details for five employees and saves them in an array. The program will continue looping until the user enters “quit”:
employee_list = [] # create an empty array list to store employee details
while True: # loop indefinitely
print("Enter employee details or type 'quit' to exit:")
name = input("Name: ") # prompt the user to enter the employee's name
if name.lower() == "quit": # check if the user has entered "quit"
break # exit the loop
age = input("Age: ") # prompt the user to enter the employee's age
role = input("Role: ") # prompt the user to enter the employee's role
salary = input("Salary: ") # prompt the user to enter the employee's salary
employee_list.append({ # add a dictionary containing the employee details to the array list
"name": name,
"age": age,
"role": role,
"salary": salary
})
# print the array list containing the employee details
print("Employee List:")
for employee in employee_list:
print(f"Name: {employee['name']}, Age: {employee['age']}, Role: {employee['role']}, Salary: {employee['salary']}")
In this code, we first create an empty array list called employee_list
to store the employee details. We then use a while
loop to continuously prompt the user to enter employee details or type “quit” to exit.
Inside the loop, we use the input()
function to ask the user to enter the employee’s name, age, role, and salary. We store the user’s input in variables called name
, age
, role
, and salary
, respectively.
We then use an if
statement to check if the user has entered “quit”. If they have, we use the break
statement to exit the loop. If they have not, we add a dictionary containing the employee details to the employee_list
array using the append()
method.
Finally, we use a for
loop to iterate over the employee_list
array and print the details for each employee.
This code does not require any imports. If you want to handle input validation or handle any errors that may occur, you may want to import the try
and except
statements from the builtins
module. For example:
from builtins import Exception, input, print
# ... rest of the code goes here
To create an employee class for this code, you can define a class
called Employee
with attributes for the employee’s name, age, role, and salary. You can then create a method called __init__
that initializes these attributes when an instance of the Employee
class is created.
Here is an example of how you can define the Employee
class:
class Employee:
def __init__(self, name, age, role, salary):
self.name = name
self.age = age
self.role = role
self.salary = salary
You can then use this class to create instances of employee objects, like this:
employee1 = Employee("John", 30, "Manager", 75000)
employee2 = Employee("Alice", 25, "Developer", 65000)
You can access the attributes of an employee object using the dot notation, like this:
print(employee1.name) # prints "John"
print(employee1.age) # prints 30
print(employee1.role) # prints "Manager"
print(employee1.salary) # prints 75000
You can also define additional methods for the Employee
class, such as a method to calculate the employee’s salary raise. For example:
class Employee:
def __init__(self, name, age, role, salary):
self.name = name
self.age = age
self.role = role
self.salary = salary
def calculate_raise(self, raise_percentage):
self.salary += self.salary * (raise_percentage / 100)
You can then use this method to calculate the employee’s salary raise like this:
employee1.calculate_raise(10) # increases the employee's salary by 10%
To use the Employee
class in the main code, you can create instances of the Employee
class and add them to the employee_list
array instead of adding dictionaries.
Here is an example of how you can modify the main code to use the Employee
class:
from builtins import input, print # import the input and print functions
employee_list = [] # create an empty array list to store employee objects
while True: # loop indefinitely
print("Enter employee details or type 'quit' to exit:")
name = input("Name: ") # prompt the user to enter the employee's name
if name.lower() == "quit": # check if the user has entered "quit"
break # exit the loop
age = input("Age: ") # prompt the user to enter the employee's age
role = input("Role: ") # prompt the user to enter the employee's role
salary = input("Salary: ") # prompt the user to enter the employee's salary
employee_list.append(Employee(name, age, role, salary)) # create a new Employee object and add it to the employee_list array
# print the array list containing the employee objects
print("Employee List:")
for employee in employee_list:
print(f"Name: {employee.name}, Age: {employee.age}, Role: {employee.role}, Salary: {employee.salary}")
In this code, we first import the input
and print
functions from the builtins
module. We then create an empty array list called employee_list
to store the employee objects.
Inside the loop, we use the input()
function to ask the user to enter the employee’s name, age, role, and salary. We store the user’s input in variables called name
, age
, role
, and salary
, respectively.
We then use the Employee
class to create a new employee object and add it to the employee_list
array using the append()
method.
Finally, we use a for
loop to iterate over the employee_list
array and print the details for each employee.
Code Output
Microsoft Windows [Version 10.0.19045.2364]
(c) Microsoft Corporation. All rights reserved.
D:\MauriceMuteti\Python\Python Employee Array Menu Driven Program>dir
Volume in drive D is New Volume
Volume Serial Number is D03F-AEA4
Directory of D:\MauriceMuteti\Python\Python Employee Array Menu Driven Program
12/23/2022 05:27 AM <DIR> .
12/23/2022 05:27 AM <DIR> ..
12/23/2022 05:27 AM 971 Employees.py
12/23/2022 05:16 AM 6,880 Python Employee Array Menu Driven Program.png
2 File(s) 7,851 bytes
2 Dir(s) 58,486,947,840 bytes free
D:\MauriceMuteti\Python\Python Employee Array Menu Driven Program>Employees.py
D:\MauriceMuteti\Python\Python Employee Array Menu Driven Program>Employees.py
Enter employee details or type 'quit' to exit:
Name: d
Age: 3
Role: r
Salary: 23
Enter employee details or type 'quit' to exit:
Name: quit
Employee List:
Name: d, Age: 3, Role: r, Salary: 23
D:\MauriceMuteti\Python\Python Employee Array Menu Driven Program>