Build a Salary Tracker - Step 32

Tell us what’s happening:

Whenever I run the test it keeps telling me I should use hasattr(self, ‘_level’) in my if statement despite the fact I’m already doing that. I tried rewriting my long if statements as two nested ifs, swapping instances of level and _level to check for errors on my side, flipped the order of comparison, but nothing seems to work. At some point it even told me I should have a fourth if statement despite the fact I did have four of them at that time. I used prints to check and it looks good so idk.

Your code so far

class Employee:
    _base_salaries = {
        'trainee': 1000,
        'junior': 2000,
        'mid-level': 3000,
        'senior': 4000,
    }

    def __init__(self, name, level):
        self.name = name
        self.level = level
        self._salary = Employee._base_salaries[level]

    def __str__(self):
        return f'{self.name}: {self.level}'

    def __repr__(self):
        return f"Employee('{self.name}', '{self.level}')"

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, new_name):
        if not isinstance(new_name, str):
            raise TypeError("'name' must be a string.")
        self._name = new_name
        print(f"'name' updated to '{self.name}'.")

    @property
    def level(self):
        return self._level

    @level.setter
    def level(self, new_level):
        if not isinstance(new_level, str):
            raise TypeError("'level' must be a string.")
        if new_level not in Employee._base_salaries:
            raise ValueError(f"Invalid value '{new_level}' for 'level' attribute.")
        if hasattr(self, '_level') and new_level == self.level:
            raise ValueError(f"'{self.level}' is already the selected level.")
        #if hasattr(self, "_level"):
        #    print(self._level)
        #    print(new_level)

# User Editable Region

        if hasattr(self, "_level") and Employee._base_salaries[new_level] < Employee._base_salaries[self._level]:
            raise ValueError("Cannot change to lower level.")

# User Editable Region

        
        self._level = new_level
    
    @property
    def salary(self):
        return self._salary

#charlie_brown = Employee('Charlie Brown', 'trainee')
#print(charlie_brown)
#print(f'Base salary: ${charlie_brown.salary}')
#charlie_brown.level = "junior"

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0

Challenge Information:

Build a Salary Tracker - Step 32

The test wants you to use Employee._base_salaries[self.level] instead of Employee._base_salaries[self._level] .

I’m not sure why this is the preference. Maybe someone else can chime in.

Edit: Checked the Python style guide. The underscore means it is preferred for internal usage.

PEP 8 – Style Guide for Python Code | peps.python.org

You are right, changing [self._level] to [self.level] makes the code pass the lesson, but as she said, even though if hasattr(self, '_level') is used, a message tells you to use it when the code doesn’t pass and it’s a different issue. That’a annoying! it was the same for step 31 too.

Thanks a lot! I can see that now. I was certain I changed it, but it seems I either forgot or I had a different problem back then. But reading the explanation, the preference makes some sense. It’d just be nicer if the feedback wasn’t misleading (which I understand can be tough since you can’t foresee every possible mistake the learner could make). I think I’ve learned my lesson on underscores now…

I recommend opening an issue on the FreeCodeCamp Github so that the challenge can be updated.