Challenge: Password Validation
Note
The comments
The new code is below
andThe new code is above
will help you to find the new code.
Opgave
Swipe to start coding
Let's implement password validation for your code!
- Define the
password
property by the@property
decorator. - The
password
property should return the_password
attribute. - Implement the password setter by the
@property.setter
decorator.
Take attention: This decorator starts from the property name (@{property_name}.setter
). - The password should be string with a length greater than or equal to
8
. - Use the
self.password
property inside the__init__()
magic method
(not a_password
).
Løsning
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class User:
is_authenticated = False
def __init__(self, username, password):
self.username = username
self.password = password
def login(self, taken_password):
if self.password == taken_password:
self.is_authenticated = True
print(f"{self.username} is authenticated")
else:
print("Wrong password!")
def logout(self):
self.is_authenticated = False
print(f"{self.username} is loggouted")
@property
def password(self):
return self._password
@password.setter
def password(self, new_password):
if isinstance(new_password, str):
if len(new_password) >= 8:
self._password = new_password
else:
print("The password length should be >= 8.")
else:
print("The password should be string.")
class Admin(User):
def create_content(self):
print(f"{self.username} creates the content")
Var alt klart?
Tak for dine kommentarer!
Sektion 3. Kapitel 9
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class User:
is_authenticated = False
def __init__(self, username, password):
self.username = username
self.___ = password
def login(self, taken_password):
if self.password == taken_password:
self.is_authenticated = True
print(f"{self.username} is authenticated")
else:
print("Wrong password!")
def logout(self):
self.is_authenticated = False
print(f"{self.username} is loggouted")
# ========== The new code is below ==========
___
def password(self):
return ___
@___.setter
def password(self, new_password):
if ___(new_password, ___):
if ___(new_password) >= ___:
self._password = new_password
else:
print("The password length should be >= 8.")
else:
print("The password should be string.")
# ========== The new code is above ==========
class Admin(User):
def create_content(self):
Spørg AI
Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat