Build a Salary Tracker - Step 39

Tell us what’s happening:

I don’t understand what is wrong with my code. It runs smoothly but the system says step 39 is failed. I’ve tried combining the steps with AND and doing it separately with 2 “if” statements nested under each opther but it still fails.

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 Employee._base_salaries[self._level] > new_salary:
            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}.')

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/150.0.0.0 Safari/537.36

Challenge Information:

Build a Salary Tracker - Step 39

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

Hi @Linbene,

Here you are checking to see if self._level has been defined. Once you know it’s defined, you can then use the level getter in your code.

Happy coding

isn’t if X and X: then Y already checking all of this? first X is checking if self._level has been defined and it won’t run second X “if” unless it’s true. i have nested it under but the same thing comes out. i don’t understand

def salary(self, new_salary):

    if not isinstance(new_salary, (int, float)):

        raise TypeError("'salary' must be a number.")

    if hasattr(self, '\_level'):

        if Employee.\_base_salaries\[self.\_level\] > new_salary:

            raise ValueError (f"Salary must be higher than minimum salary ${Employee.\_base_salaries\[self.\_level\]}.")

    self.\_salary = new_salary

    print(f'Salary updated to ${self.\_salary}.')

using getter later instead of raw attribute as per your advice. still fails:(

if hasattr(self, '\_level') and Employee.\_base_salaries[self.level] > new_salary:

        raise ValueError(f"Salary must be higher than minimum salary ${Employee.\_base_salaries[self.level]}.")

    self.\_salary = new_salary

You are still not using the level getter after hasattr(self, '\_level') evaluates to True.

create another one for when the new salary is less than the base salary for the current level.

Are you doing this?

oookay, so i just had to switch to new_salary < Employee._base_salaries[self.level]. but why is the wording so important? i had base_salary > new_salary before that, doing the same thing. is it just the quirk of the exercise?