Build a Salary Tracker - Step 32

Tell us what’s happening:

if hasattr(self, ‘_level’) and new_level < self.level:
raise ValueError(“Cannot change to lower level.”)

I already put the hasattr() function and it still doesn’t work. It shows the following message: Your if statement should use hasattr(self, ‘_level’) to check if _level exists before comparing.

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.")

# User Editable Region

        if hasattr(self, '_level') and new_level < 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}')

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

Challenge Information:

Build a Salary Tracker - Step 32

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

here you are comparing two strings with <, is that what you are asked to do?

I changed it now to:

if hasattr(self, ‘_level’) and self._base_salaries[new_level] < self.salary:

        raise ValueError("Cannot change to lower level.")

and I moved the salary property on top of the setter.

It still gave me a warning: Your if statement should use hasattr(self, '_level') to check if _level exists before comparing.

remember that _base_salaries is not an instance attribute it is a class attribute

I changed it to

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

However, the same error occured:
Your if statement should use hasattr(self, '_level') to check if _level exists before comparing.

which of those is the base salary of the current level?

I got it now, I replaced self.salary with Employee._base_salaries[self.level]

here the code:
removed by moderator

Welcome to the forum @creator1!

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.

Happy coding!

Didn’t know about that, never use forum before, so it won’t happen again :slight_smile: