Build a Salary Tracker - Step 14

Tell us what’s happening:

I can’t figure out why it doesn’t like my if statement. I’ve tried a number of variations of type(name) != str or type(level) != str, with and without parenthesis, and it correctly raises the error if I put a int instead of a str.

Your code so far

class Employee:

# User Editable Region

    def __init__(self, name, level):
        if not type(name) == str or not type(level) == str:
            raise TypeError ("'name' and 'level attribute must be of type 'str'.")
        else:
            self._name = name
            self._level = level
    def __str__(self):

# User Editable Region

        return f'{self.name}: {self.level}'

    def __repr__(self):
        return f"Employee('{self.name}', '{self.level}')"

    @property
    def name(self):
        return self._name

    @property
    def level(self):
        return self._level

charlie_brown = Employee('Charlie Brown', 2)
print(charlie_brown)

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build a Salary Tracker - Step 14

Here is the end of my console log, idk if Unspecified AssertionErrroractual: null, expected: undefined has something to do with it.

Charlie Brown: trainee 3 python-test-evaluator.js:2:12859
Employee(Charlie Brown, trainee) python-test-evaluator.js:2:12859
Charlie Brown: trainee python-test-evaluator.js:2:12859
Employee(Charlie Brown, trainee) python-test-evaluator.js:2:12859
Charlie Brown: trainee python-test-evaluator.js:2:12859
Employee('Charlie Brown', 'trainee') python-test-evaluator.js:2:12859
Charlie Brown: trainee python-test-evaluator.js:2:12859
Employee('Charlie Brown', 'trainee') python-test-evaluator.js:2:12859
progress updates processed where possible failed-updates-epic.js:92:29
Registration failed xterm.tsx:14:15
TypeError: ServiceWorker script at https://www.freecodecamp.org/python-input-sw.js for scope https://www.freecodecamp.org/ threw an exception during script evaluation. xterm.tsx:15:15
Employee('Charlie Brown', 'trainee') python-test-evaluator.js:2:12859
progress updates processed where possible failed-updates-epic.js:92:29
Registration failed xterm.tsx:14:15
TypeError: ServiceWorker script at https://www.freecodecamp.org/python-input-sw.js for scope https://www.freecodecamp.org/ threw an exception during script evaluation. xterm.tsx:15:15
Charlie Brown: trainee python-test-evaluator.js:2:12859
progress updates processed where possible failed-updates-epic.js:92:29
Registration failed xterm.tsx:14:15
TypeError: ServiceWorker script at https://www.freecodecamp.org/python-input-sw.js for scope https://www.freecodecamp.org/ threw an exception during script evaluation. xterm.tsx:15:15
Charlie Brown: trainee 3 python-test-evaluator.js:2:12859
Object { message: "Unspecified AssertionError", showDiff: false, actual: null, expected: undefined }
python-test-evaluator.js:2:158899
Referrer Policy: Ignoring the less restricted referrer policy “no-referrer-when-downgrade” for the cross-site request: https://qmjyl5wyti-dsn.algolia.net/1/indexes/*/queries?x-algolia-agent=Algolia%20for%20JavaScript%20(4.22.1)%3B%20Browser%20(lite)%3B%20instantsearch.js%20(4.75.3)%3B%20react%20(17.0.2)%3B%20react-instantsearch%20(7.13.6)%3B%20react-instantsearch-core%20(7.13.6)%3B%20JS%20Helper%20(3.22.5)&x-algolia-api-key=f91afff73d62604d4df9ae9046e8ca23&x-algolia-application-id=QMJYL5WYTI queries
Charlie Brown: trainee 3 python-test-evaluator.js:2:12859
Object { message: "Unspecified AssertionError", showDiff: false, actual: null, expected: undefined }
python-test-evaluator.js:2:158899
Charlie Brown: trainee 3 python-test-evaluator.js:2:12859
Object { message: "Unspecified AssertionError", showDiff: false, actual: null, expected: undefined }
python-test-evaluator.js:2:158899
Charlie Brown: trainee 3 python-test-evaluator.js:2:12859
Object { message: "Unspecified AssertionError", showDiff: false, actual: null, expected: undefined }

pay attention to the wording

are not instances of

what is the way to check if something is an instance of something else?

If you read my comment above, you will see I did try variations with not.

This error message is ambiguous. ‘either’ implies ‘or’ not ‘and’ so I am confused:

2. Your if statement should check if either name and level are not instances of str.

for example:


        if not (type(name) == str and type(level) == str):
        #if type(name) != str or type(level) != str:
        #if not type(name) == str or not type(level) == str:

Returns `You should have an if statement in your __init__ method.` despite there being an if statement.

Oh I see, isinstance does more than check type I guess. It’s still giving me `You should have an if statement in your __init__ method.` error, when I do have the if statement there and functioning as expected.

Please share your updated code

The output showing it is working:

Traceback (most recent call last):
  File "main.py", line 27, in <module>
  File "main.py", line 9, in __init__
TypeError: 'name' and 'level attribute must be of type 'str'.

Your code so far

class Employee:

# User Editable Region

    def __init__(self, name, level):
        #if not (type(name) == str or not type(level) == str):
        if not (isinstance(name, str) and isinstance(level, str)):
        #if type(name) != str or type(level) != str:
        #if not type(name) == str or not type(level) == str:


            raise TypeError ("'name' and 'level attribute must be of type 'str'.")
        else:
            self._name = name
            self._level = level
    def __str__(self):

# User Editable Region

        return f'{self.name}: {self.level}'

    def __repr__(self):
        return f"Employee('{self.name}', '{self.level}')"

    @property
    def name(self):
        return self._name

    @property
    def level(self):
        return self._level

charlie_brown = Employee('Charlie Brown', 2)
print(charlie_brown)

The error I’m getting again is:

You should have an if statement in your __init__ method.

Challenge Information:

Build a Salary Tracker - Step 14

1 Like

Logic is good you are just missing some punctuation in the error string.

Totally missed that, thanks. But it is still giving me the same “you should have an if statement” error (when the if statement has been there the whole time).

Your code so far

class Employee:

# User Editable Region

    def __init__(self, name, level):
        #if not (type(name) == str or not type(level) == str):
        if not (isinstance(name, str) and isinstance(level, str)):
        #if type(name) != str or type(level) != str:
        #if not type(name) == str or not type(level) == str:


            raise TypeError ("'name' and 'level' attribute must be of type 'str'.")
        else:
            self._name = name
            self._level = level
    def __str__(self):

# User Editable Region

        return f'{self.name}: {self.level}'

    def __repr__(self):
        return f"Employee('{self.name}', '{self.level}')"

    @property
    def name(self):
        return self._name

    @property
    def level(self):
        return self._level

charlie_brown = Employee('Charlie Brown', 2)
print(charlie_brown)

Where does it ask for an else: clause in the instructions?

Your if code passes the test. Reset the step if you need to.

I figured out it was the line at the end I was using to test that if statement, charlie_brown = Employee('Charlie Brown', 2). But still strange that the error was saying I was missing the if statement.

An error is just an error, it cannot give accurate feedback for any input.

Pay attention to the instructions, not the test.