Build a Salary Tracker - Step 31

Tell us what’s happening:

I’m having trouble understanding if the code checking the hasattr(self, ‘_level’) is formatted incorrectly on my end or if there’s missing information in the instructions. As far as I can tell this nested if statement is exactly what’s being asked for. I feel like I’m missing something that’s staring me in the face here

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

# User Editable Region

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0

Challenge Information:

Build a Salary Tracker - Step 31

For additional context, the browser inspector is showing an unspecified assertion error when testing the exception raise for assigning a new level as the same value as the current level. It looks like it’s seeing the raised error as incorrect even though that should be the expected outcome

is the one who wrote the code validation is a fresher or is playing with us as →

  1. ) if hasattr(self, ‘_level’) and self._level == new_level:

         raise ValueError(f"'{self.\_level}' is already the selected level.")  
    

–> this gave me errror –> Your if statement should use hasattr(self, '_level') to check if _level exists before comparing.
2. ) if hasattr(self, ‘__level’) and self._level == new_level:

        raise ValueError(f"'{self.\_level}' is already the selected level.")

–> the first error gone and this new error encountered –>
When new_level is equal to self.level, you should raise a ValueError with the message '{level}' is already the selected level., where {level} should be replaced by the current level.
–> i checked everything every spell check like i didn’t wrote anything wrongly as in the init() method also that was exactly self.level for setter and just after this troubling condition in setter there is self._level=new_level, i asked gemini also that also gave me suggestion about the passing conditions may be strict like writing order, as nested if statement and this and that bla bla and i have tried all of that but nothing worked, i think there is a hidden condition or something they forgot to write can someone help if anyone found solution?

i found the solution and i can’t expect this kind of stupidity as if you are expecting answer in a particular order that here a=5,b=5 then a==b will give an error but b==a will run so you must provide such stupid cases so we do not waste our time as i wasted nearly half an hour in this stupid error and we can’t use the variable instead we must use the getter to get value so finally the correct answer is this →
if hasattr(self, ‘_level’) and new_level==self.level:

        raise ValueError(f"'{self.level}' is already the selected level.")

–> even though output of my previous code was correct and the previous one wouldn’t have raised any exception because two conditions which are we performing in getter is already in setter above this condition but i don’t know why these kind of time wasting restrictions?

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.


we try to allow all permutations that are correct, but when writing the tests and reviewing them it’s hard to keep in mind every possible variation, when they escape it’s really helpful if campers like you can open an issue on github so that the tests can be improved.

I can’t believe it was as simple as the syntax returning the wrong output. I did discover that even with the correct syntax, calling the getter after the new_level in the inequality as a second, nested if statement also returns the same error stating that the if statement should use hasattr. Really saved my skin on this