Build a Salary Tracker - Step 31

Tell us what’s happening:

Bug Report – Step 31 grader not accepting correct code
The grader for Step 31 keeps returning: “Your if statement should use hasattr(self, ‘_level’) to check if _level exists before comparing.”
However, my code already does exactly that. I have tried both of the following approaches and neither is accepted:

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' attribute must be of type 'str'.")

        if new_level not in Employee._base_salaries:
            raise ValueError(f"Invalid value '{new_level}' for 'level' attribute.")


# User Editable Region

        if hasattr(self, '_level') and self._level == new_level:
            raise ValueError(f"'{new_level}' is already the selected level.")

        self._level = new_level
        self._salary = Employee._base_salaries[new_level]

# User Editable Region

    

    @property
    def salary(self):
        return self._salary

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

Your browser information:

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

Challenge Information:

Build a Salary Tracker - Step 31

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/workshop-salary-tracker/68c9cab4b1118da59eecfc56.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @Sanmiayodeji,

You are checking to see if self._level exists yet, right? And if it does, the second part of your condition should use the level getter (but the tests expect the variables in your equality check to be swapped). Also, use the level getter in the error message to pass the tests.

Happy coding

just do as i done and passed it:

removed by moderator

Our logics are correct but test needs specific way to code so it asks for that way.

Happy Coding!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge. How to Help Someone with Their Code Using the Socratic Method

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Yes, I tried too.

In the condition, We need to use self.level instead self._level.
and should swap position like new_level == self.level.
I don’t know reason correctly.