jeudi 3 décembre 2015

How to best store individual employee records using lists and tuples with python

Currently I'm working on a university assignment and I seem to have hit a wall, my code at the moment asks the user for an option and if that option is to add an employee it will do that however once several employees have been added it gives the following structure: [['Test1', 'Test2'], ['CEO', 'Cleaner'], ['y', 'y'], [12.0, 3.0], [2.0, 1.0]] meaning [name], [position], [fulltime / not], [wage p/h], [years of service] but I do not know how to make it so each employee would have there own list which is embedded in a tuple as instructed to me that all employee data has to been stored in a tuple but the tuple will contain lists. Any code related help please keep the syntax simple as we are still learning the language and have not been introduced to many advanced functions.

My code at the moment:

#Assignment 1
name = []
position = []
fulltime = []
pay = []
years = []
employee = [name, position, fulltime, pay, years]
employeelist = (employee)
ans = True
while ans:
    print()
    print("Human Resources department")
    print("Personnel Database (Employee records:")
    print("+-+-+-+-+-+-+-+-+")
    print("""
a. Add a new employee record
b. Display all employees
c. Search for employee record
d. Delete an employee record
e. Show all full-time employees
f. Show all part-time employees
x. Exit
Choose an option (a to f) or x to Exit 
""")
    ans = input("Select an option: ")
    if ans == "a":
        print("Option 1 - Selected")
        print()
        #Add new employee to the list
        person = input("Enter employee name: ")
        if person not in name:
            name.append(person)

        #Add new employee job title
        job = input("Enter job title: ")
        if job not in position:
            position.append(job)

        #decide if employee is full time or not
        ft = input("Full time employee? (y/n): ")

        #validate the input
        if ft != "y" and ft != "n":
            print("Invalid input")

        #loop until the input is valid
            while ft != "y" and ft != "n":
                ft = input("Full time employee? (y/n): ")      
        #store in list if valid

        else:
            fulltime.append(ft)

        #Find the employees hourly pay
        money = float(input("What is the employees hourly pay: £"))
        if type(money) == float:
            pay.append(money)

        #Years of service
        yos = float(input("Years of service: "))
        if type(yos) == float:
            years.append(yos)
            print()
            print("Employee record stored")
            print()


    if ans == "b":
        print("Option 2 - Selected")
        print()
    #display all employees
        employeenumber = len(name)
        print("Number of employees: ", employeenumber)
        print(employeelist)

    if ans == "c":
        print("Option 3 - Selected")
        print()
        #Search function
    if ans == "d":
        print("Option 3 - Selected")
        print()
        #Delete employee record



    if ans == "x":
        print("GoodBye")
        quit()

I'm only really concerned with option a at the moment because I feel like once I'm able to use option a in the way it is suppose to be used I will be able to complete the rest.

This is the first part of my task followed by a 1600 word report just in case anyone is curious:

" Task (Personnel Database): • Write a Python script to create and manage a very basic personnel database for a humanresources department in an organisation.

• The personnel database should be stored in a simple Python list, where each item in the list is one employee record. Additionally, each employee record should be stored in a Python tuple.

o An employee record should consist of: Employee name Job title Full time (y/n) Hourly rate (in £) Number of full years service

• Write some code that allows the user to enter the details for a new employee record; remember to consider the data type for each item in the employee record. This code should also add the new employee record (Python tuple) to the personnel database (Python list).

• Write some code that will display, in a suitably formatted list, the names of all the employees stored in the database.

• Now write some code to add a menu system to your program and therefore allow the user to select options to add further employees and re-display the list of names. The menu at this stage should look similar to the following:

Human Resources department Personnel Database +-+-+-+-+-+-+-+-+- a. Add a new employee b. Display all employees c. Exit Choose an option (a to c) • Add an option to your program to allow the user to search the database for an individual employee (by name) and display the employee record of it in a suitable format.

• Add an option to your program to allow the user to delete an entry from the database (i.e.remove an employee record).

• Add two more options to the main menu to allow the user to view a filtered list of all full-time employees and a filtered list of all part-time employees.

• Finally add some code to display on the main menu how many employee records are currently loaded into the personnel database.

• When you are finished you should have a program with a repeating menu system which looks like the following:

Human Resources department

Personnel Database (Employee records: X)

+-+-+-+-+-+-+-+-+-

a. Add a new employee record

b. Display all employees

c. Search for employee record

d. Delete an employee record

e. Show all full-time employees

f. Show all part-time employees

x. Exit

Choose an option (a to f) or x to Exit

All options should return to this menu when completed (apart from the last one). Advanced Tasks:

• Add two further options to the main menu of the program to allow the user to:

o View a sorted list of all employees, sorted alphabetically by employee name.

o Search for a job title and display a list of employees with a matching job title.

"

Aucun commentaire:

Enregistrer un commentaire