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