AJN
AJN

Reputation: 1206

Python class attribute counter doesn't increment properly

I am testing Python OOP concept of class and instance attributes.

Goal:

Increase the total number of employees (class attribut) each time an instance of the class "Employee" is created.

class Employee:

    num_of_emps = 0
    raise_amt = 1.04

    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.email = first + '.' + last + '@email.com'
        self.pay = pay

        Employee.num_of_emps =+ 1

emp_1 = Employee('Brian', 'Rigs', 65000)
emp_2 = Employee('Eric', 'Masson', 65000)
emp_3 = Employee('John', 'Doe', 80000)

print(Employee.num_of_emps)

Expected Result: Employee.num_of_emps should be equal to 3

Actual result:

print(Employee.num_of_emps)
1

I should have missed something, any hint?

Upvotes: 0

Views: 97

Answers (1)

Chris
Chris

Reputation: 1668

You just have a minor typo – change Employee.num_of_emps =+ 1 to Employee.num_of_emps += 1. (I.e. =+ to +=).

Upvotes: 3

Related Questions