Challenge: User Auth
Aufgabe
Swipe to start coding
Let's improve your User
class!
- Create the class attribute
is_authenticated
in theUser
class. - Define the
login()
method that takes argumentsself
andtaken_password
. - The
login()
method should compare the user and taken passwords.
If the user password is equal to the taken password, theTrue
should be assigned to theis_authenticated
attribute.
If the user password is not equal to the taken password, the"Wrong password!"
should be written in the console. - Define the
logout()
methods. This method should assign theFalse
value to theis_authenticated
instance attribute.
Lösung
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
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("User is authenticated")
else:
print("Wrong password!")
def logout(self):
self.is_authenticated = False
print("User is loggouted")
bob = User("bob123.user", "secret_bob_password")
print(bob.is_authenticated)
bob.login("123wsad123")
bob.login("secret_bob_password")
print(bob.is_authenticated)
bob.logout()
print(bob.is_authenticated)
War alles klar?
Danke für Ihr Feedback!
Abschnitt 1. Kapitel 8
single
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
class User:
___ = False
def __init__(self, username, password):
self.username = username
self.password = password
def ___(self, ___):
if self.___ == taken_password:
self.is_authenticated = ___
print("User is authenticated")
else:
print("Wrong password!")
def ___(self):
self.___ = False
print("User is loggouted")
bob = User("bob123.user", "secret_bob_password")
print(bob.is_authenticated) # False
bob.login("123wsad123") # Try to login with wrong password
bob.login("secret_bob_password")
print(bob.is_authenticated) # True
bob.logout()
print(bob.is_authenticated) # False
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen