Build a Salary Tracker - Step 39

Tell us what’s happening:

I inserted the following snippet which passes the test:
if hasattr(self, ‘_level’) and new_salary < self._base_salaries[self.level]:
raise ValueError(f"Salary must be higher than minimum salary ${Employee._base_salaries[self.level]}.")

However I get an error: Your if statement should use hasattr(self, ‘_level’) to check if _level exists before comparing.

this error is nonsense since I obviously included hasattr(self, 'level')

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') and Employee._base_salaries[new_level] < Employee._base_salaries[self.level]:
            raise ValueError("Cannot change to lower level.")
        print(f"'{self.name}' promoted to '{new_level}'.")
        self.salary = Employee._base_salaries[new_level]
        self._level = new_level

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

    @salary.setter
    def salary(self, new_salary):
        if not isinstance(new_salary, (int, float)):
            raise TypeError("'salary' must be a number.")

# User Editable Region

        if hasattr(self, '_level') and new_salary < self._base_salaries[self.level]:
            raise ValueError(f"Salary must be higher than minimum salary ${Employee._base_salaries[self.level]}.") 

# User Editable Region

        self._salary = new_salary
        print(f'Salary updated to ${self.salary}.')

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36

Challenge Information:

Build a Salary Tracker - Step 39

Hi @dwo,

Sometimes error messages don’t point to the exact issue.

But notice you’re writing self._base_salaries[self.level] in your if condition and Employee._base_salaries[self.level] in the message.

Happy coding!

Your if statement should use hasattr(self, '_level') to check if _level exists before comparing. i am getting this message

Please create your own topic when you have specific questions about your own challenge code. Only respond to another thread when you want to provide help to the original poster of the other thread or have follow up questions concerning other replies given to the original poster.

The easiest way to create a topic for help with your own solution is to click the Help button image located on each challenge. This will automatically import your code in a readable format and pull in the challenge URL while still allowing you to ask any question about the challenge or your code.

Thank you.