Build a Salary Tracker - Step 31

Tell us what’s happening:

After the existing if statements, create another one to raise a ValueError when new_level is already the selected level.

Note that _level doesn’t exist yet during initialization, so use hasattr(self, ‘_level’) to check if it exists before comparing. This avoids an AttributeError when the object is first created.

For the message, use ‘{level}’ is already the selected level., where {level} should be replaced by the current 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:

# User Editable Region

            raise ValueError(f"Invalid value '{new_level}' for 'level' attribute.")

# User Editable Region

        self._level = new_level
        if hasattr(self, '_level') and self._level == new_level:
            raise ValueError(f"'{self._level}' is already the selected 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/148.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

you are writing the validation after setting the value of self._level, is that correct?

also use the getter

solution :slight_smile:

code removed by moderator

Here’s why:

self._level — direct access to the private attribute self.level goes through the property getter, which returns self._level

Both give the same value, but the grader likely checks the source code text for the exact pattern self.level (the public property), because the exercise says "the base salary for the current level" and self.level is the public way to access the current level

hi @marwen.maghrebi

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.

Hi @ILM,

I understand your point about avoiding full solutions, and I respect the goal of encouraging learning.

That said, my intention was to clarify a specific detail that could help others understand the issue more clearly, not just to give away an answer. I believe there’s value in showing concrete examples when they directly address confusion.

I’ll keep your recommendation in mind for future posts, but I also think there’s room for balanced contributions that both guide and clarify.

Thanks for your input.

To be clear, this post of you I think was pretty good

I had to edit the other post where you gave the full solution that could be copied and pasted