Challenge: Protected Password
Opgave
Swipe to start coding
Wow, here is our User
class again! Let's protect the password!
- All
password
attributes and arguments were replaced by the placeholder (___
) to help you not miss some attributes. - Use the protected access modifier (
_
) to protect thepassword
attribute.
Note
Arguments taken by functions should be without protected access modifier.
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")
class Admin(User):
def create_content(self):
print(f"{self.username} creates the content")
def update_content(self):
print(f"{self.username} updates the content")
def delete_content(self):
print(f"{self.username} deletes the content")
frank = Admin("frank.admin", "secret.admin.pswrd")
frank.login("secret.admin.pswrd")
frank.create_content()
frank.update_content()
frank.delete_content()
frank.logout()
Var alt klart?
Tak for dine kommentarer!
Sektion 3. Kapitel 5
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, ___):
self.username = username
self.___ = ___
def login(self, taken_password):
if self.___ == 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")
class Admin(User):
def create_content(self):
print(f"{self.username} creates the content")
def update_content(self):
print(f"{self.username} updates the content")
def delete_content(self):
print(f"{self.username} deletes the content")
frank = Admin("frank.admin", "secret.admin.pswrd")
frank.login("secret.admin.pswrd")
frank.create_content()
frank.update_content()
frank.delete_content()
frank.logout()
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