Question

How to add class objects to a dictionary? *PYTHON ONLY* NO IMPORT RANDOM Write a class...

How to add class objects to a dictionary? *PYTHON ONLY* NO IMPORT RANDOM

Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict (outside of the Employee class) that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses. The function should take the first value of each list and use them to create an Employee object. It should do the same with the second value of each list, etc. to the end of the lists. As it creates these objects, it should add them to a dictionary, where the key is the ID number and the value for that key is the whole Employee object. The function should return the resulting dictionary.

This is what I got so far:

class Employee:

    """
    Represents the employee's name, ID_number, salary, and email_address.
    """
    def __init__(self, name, ID_number, salary, email_address):
        self.name = name
        self.ID_number = ID_number
        self.salary = salary
        self.email_address = email_address

def make_employee_dict(names, IDs, salaries, emails):
    """
    This function "make_employee_dict" takes the first value of each list and uses them to create
    an Employee object. It does the same with the second value of each list, etc. to the end of the
    the lists. This where I'm stuck at :/
    """

If I input:

names = ["Ann", "Gabriel", "Maria"]
IDs = [10, 11, 12]
salaries = [35, 29, 35]
emails = ["ann@blah.com", "gabriel@blah.com", "maria@blah.com"]
print(make_employee_dict(names, IDs, salaries, emails))

Expected output:

{10: 'Ann', 10, 35, 'ann@blah.com', 11: 'Gabriel', 11, 29, 'gabriel@blah.com', 12: 'Maria', 35, 'maria@blah.com'}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# do comment if any problem arises

# Code


class Employee:

    """

    Represents the employee's name, ID_number, salary, and email_address.

    """

    def __init__(self, name, ID_number, salary, email_address):

        self.name = name

        self.ID_number = ID_number

        self.salary = salary

        self.email_address = email_address

    def __repr__(self):

        return str(self.name)+", " + str(self.ID_number)+", "+str(self.salary)+", "+str(self.email_address)


def make_employee_dict(names, IDs, salaries, emails):

    """

    This function "make_employee_dict" takes the first value of each list and uses them to create

    an Employee object. It does the same with the second value of each list, etc. to the end of the

    the lists. This where I'm stuck at :/

    """

    # create empty dictionary

    employee_dict = {}

    for i in range(len(names)):

        # create an employee

        employee = Employee(names[i], IDs[i], salaries[i], emails[i])

        # add to employee dictionary

        employee_dict[IDs[i]] = employee

    return employee_dict


# input

names = ["Ann", "Gabriel", "Maria"]

IDs = [10, 11, 12]

salaries = [35, 29, 35]

emails = ["ann@blah.com", "gabriel@blah.com", "maria@blah.com"]

print(make_employee_dict(names, IDs, salaries, emails))

Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
How to add class objects to a dictionary? *PYTHON ONLY* NO IMPORT RANDOM Write a class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary,...

    project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object....

  • Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for...

    Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...

  • Define the functions in Python 3.8 1. Write a function most frequent n that takes a...

    Define the functions in Python 3.8 1. Write a function most frequent n that takes a list of strings and an integer n, and that returns a dictionary where the keys are the top n most frequent unique words in the list, and the values are the frequency of each word: For example, most frequent n(text, 3) should return the dictionary {'is': 2, "the’: 3, 'of': 2}, and most frequent n(text, 2) could return either {'is': 2, 'the’: 3} or...

  • In header file (.h) and c++ file format (.cpp). A local company has asked you to...

    In header file (.h) and c++ file format (.cpp). A local company has asked you to write a program which creates an Employee class, a vector of Employee class objects, and fills the objects with employee data, creating a "database" of employee information. The program allows the user to repeatedly search for an employee in the vector by entering the employee's ID number and if found, display the employee's data. The Employee_C class should have the following data and in...

  • A) Please implement a Python script to define a student class with the following attributes (instance...

    A) Please implement a Python script to define a student class with the following attributes (instance attributes): cwid: student’s CWID first_name : student’s first name last_name: student’s last name gender: student’s gender gpa: student’s gpa Please make these attributes as private ones so they have to be accessed via getter/setter methods or property. For this purpose, please define a getter/setter method and property for each of the attributes. Another thing you need to do is to define a constructor that...

  • It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming...

    It must be C++ Chapter 13 Programming Challenge 2: Employee Class. See instruction: Chapter 13 Programming Challenge 2 Employee Class.pdf Program Template: // Chapter 13, Programming Challenge 2: Employee Class #include <iostream> #include <string> using namespace std; // Employee Class Declaration class Employee { private: string name; // Employee's name int idNumber; // ID number string department; // Department name string position; // Employee's position public: // TODO: Constructors // TODO: Accessors // TODO: Mutators }; // Constructor #1 Employee::Employee(string...

  • Zander Inc. stores employee IDs and salaries in a sequential access file named Employees.txt. Open the...

    Zander Inc. stores employee IDs and salaries in a sequential access file named Employees.txt. Open the VB2015\Chap10\Zander Solution\Zander Solution (Zander Solution.sln) file. Open the Employees.txt file, which is contained in the project’s bin\Debug folder. The ID and salary information appear on separate lines in the file. Close the Employees.txt window. a. Define a structure named Employee. The structure should contain two member variables: a String variable to store the ID and a Double variable to store the salary. b. Declare...

  • Write this code on python 3 only. Suppose class Student represents information about students in a...

    Write this code on python 3 only. Suppose class Student represents information about students in a course. Each student has a name and a list of test scores. The Student class should allow the user to view a student's name, view a test score at a given position, view the highest test score, view the average test score, and obtain a string representation of the student's information. When a Student object is created, the user supplies the student's name and...

  • in C language we need to find the following : EXERCISE 3: 1. Write the definition...

    in C language we need to find the following : EXERCISE 3: 1. Write the definition of a structure named employee that contains character array members for an employee's first and last names, an int member for the employee's age, a char member that contains 'M' or 'F' for the employee's gender, and a double member for the employee's hourly salary. 2. Using the above mentioned definition, write a C program that asks a user to enter the values of...

  • Suppose you were given the job to write a Java class to manage employee payroll information...

    Suppose you were given the job to write a Java class to manage employee payroll information in an HR management system. Each employee will be modeled as a single EmployeePayroll object (much like the Student objects discussed in class). The state of the EmployeePayroll object includes the first name and last name of the employee, a unique 15 digit employee number for each employee, a number of hours that they've worked this week (which could be a fraction of an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT