Thonny the Employee

Employees: Building a Class from Scratch

In this activity you’ll write an Employee class from scratch in Thonny, use it from a second file, and then extend it step by step. Each step tells you what to write and shows the output you should get. Type everything yourself. Don’t copy and paste.

Include your answers to the questions as comments in your python file. You’ll submit the python files through Moodle.

Before you start: make a folder called employee_project in your Documents folder. Every file in this activity goes in that folder.


Step 1: Write the Employee Class

Open Thonny. Choose File → New, then File → Save as, open your employee_project folder, and save the empty file as employee.py (all lowercase).

Now write the class, one piece at a time:

  1. The class header. Start a class named Employee. Everything else in this step goes indented inside it.
  2. The employee count. Directly inside the class (not inside any method), create a variable named empCount and set it to 0. A variable created here belongs to the whole class: there is one copy, shared by every employee. You’ll use it to count how many employees have been created.
  3. The __init__ method. Write an __init__ method with three parameters: self, name, and salary. Inside it:
    • store name on the object as self.name
    • store salary on the object as self.salary
    • add 1 to the shared count. Because the count belongs to the class, refer to it as Employee.empCount, not self.empCount.
  4. The displayCount method. Write a method displayCount(self) that prints Total Employees: followed by Employee.empCount.
  5. The displayEmployee method. Write a method displayEmployee(self) that prints this employee’s name and salary, like this: Name: Bill Yeakus Salary: 50000

Save the file and click the green Run button. If you see no error, your class is written correctly so far. (It’s normal that nothing prints. You’ll see why in Q2.)

Q1. name and salary are stored with self., but empCount is not. If you created three employees, how many separate copies of name would exist? How many copies of empCount?


Step 2: Use the Class from Another File

Create a second file (File → New) and save it as main.py in the same employee_project folder. This file will use your class.

  1. The first line of main.py must import your class from your file: from employee import Employee
  2. Create three employee objects named emp1, emp2, and emp3:
    • Bill Yeakus, salary 50000
    • Sally Smith, salary 75000
    • Joan Jones, salary 66000
  3. Call displayEmployee() on each of the three.
  4. Call displayCount() on any one of them.

Save, make sure the main.py tab is selected (Thonny runs whichever tab is selected), and click Run.

Expected output

Name: Bill Yeakus Salary: 50000
Name: Sally Smith Salary: 75000
Name: Joan Jones Salary: 66000
Total Employees: 3

Q2. Select the employee.py tab and click Run. Why does nothing print, even though displayEmployee has a print statement in it?

Q3. In from employee import Employee, which word refers to your file and which refers to your class?


Step 3: Add an ID, Position, and Department

The company wants every employee to also have a position, a department, and a unique ID number. IDs start at 1 and are assigned automatically: the first employee created gets 1, the next gets 2, and so on. Nobody types an ID in.

In employee.py:

  1. Next to empCount, create a second class variable named next_id and set it to 1. It remembers which ID to hand out next.
  2. Add two parameters to __init__, after salary: position and department. Store both on the object with self., the same way you stored name and salary.
  3. Still inside __init__, give this employee their ID: set self.id to the current value of Employee.next_id.
  4. Then add 1 to Employee.next_id so the next employee gets the next number.

In main.py, your three Employee(...) lines now need a position and department too:

EmployeePositionDepartment
Bill YeakusEngineerDevelopment
Sally SmithManagerSales
Joan JonesAnalystFinance

To check the IDs, add this line to the end of main.py and run it. It should print 1 2 3.

print(emp1.id, emp2.id, emp3.id)

Q4. Why is id not one of the parameters of __init__, like position and department are?

Q5. Why must next_id be a class variable? Try changing Employee.next_id += 1 to self.next_id += 1. What IDs print now? (Change it back afterward.)


Step 4: Display an Employee with an f-string

An f-string is a string with an f right before the opening quote. Anything inside curly braces { } is replaced with its value. For example, if city is "Gambier", then f"I live in {city}!" becomes I live in Gambier!

In employee.py, rewrite displayEmployee so it uses one f-string to print all of this employee’s information in exactly this format:

ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000

In main.py, delete the print(emp1.id, ...) line and run it.

Expected output

ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000
ID: 2, Name: Sally Smith, Position: Manager, Department: Sales, Salary: $75000
ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $66000
Total Employees: 3

Q6. Remove the f in front of your f-string and run it. What prints instead? Why? (Put the f back afterward.)


Step 5: Managers and Their Teams

Some employees are managers, and other employees report to them. You’ll give every employee a list of the people who report to them, plus two methods for working with that list.

In employee.py:

  1. In __init__, create an attribute self.employees and set it to an empty list. Every employee starts with nobody reporting to them.
  2. Write a method add_employee(self, employee). It should add the employee it’s given to this employee’s self.employees list, then use an f-string to print a message like: Bill Yeakus now reports to Sally Smith
  3. Write a method list_employees(self). It should print a heading like Sally Smith's team:, then loop through self.employees and call displayEmployee() on each one.

In main.py, add lines at the end so that Bill and Joan both report to Sally (the manager), then list Sally’s team.

Expected output (after the lines from Step 4)

Bill Yeakus now reports to Sally Smith
Joan Jones now reports to Sally Smith
Sally Smith's team:
ID: 1, Name: Bill Yeakus, Position: Engineer, Department: Development, Salary: $50000
ID: 3, Name: Joan Jones, Position: Analyst, Department: Finance, Salary: $66000

Q7. Add a line that lists Bill’s team. What prints? Why?

Q8. The add method is called on one employee and given another employee as its argument. In emp2.add_employee(emp1), which employee is self, and which one is employee?


Wrap-Up

Q9. Your class now stores some values on self and some on the class. List which variables went where, and explain in one sentence how you decided.

Scroll to Top