71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
class User:
|
|
"""Represent a simple user profile."""
|
|
|
|
def __init__(self, first_name, last_name, username, email, location):
|
|
"""Initialize the user."""
|
|
self.first_name = first_name.title()
|
|
self.last_name = last_name.title()
|
|
self.username = username
|
|
self.email = email
|
|
self.location = location.title()
|
|
self.login_attempts = 0
|
|
|
|
def describe_user(self):
|
|
"""Display a summary of the user's information."""
|
|
print(f"\n{self.first_name} {self.last_name}")
|
|
print(f" Username: {self.username}")
|
|
print(f" Email: {self.email}")
|
|
print(f" Location: {self.location}")
|
|
|
|
def greet_user(self):
|
|
"""Display a personalized greeting to the user."""
|
|
print(f"\nWelcome back, {self.username}!")
|
|
|
|
def increment_login_attempts(self):
|
|
"""Increment the value of login_attempts."""
|
|
self.login_attempts += 1
|
|
|
|
def reset_login_attempts(self):
|
|
"""Reset login_attempts to 0."""
|
|
self.login_attempts = 0
|
|
|
|
|
|
class Admin(User):
|
|
"""A user with administrative privileges."""
|
|
|
|
def __init__(self, first_name, last_name, username, email, location):
|
|
"""Initialize the admin."""
|
|
super().__init__(first_name, last_name, username, email, location)
|
|
|
|
# Initialize an empty set of privileges.
|
|
self.privileges = Privileges()
|
|
|
|
|
|
class Privileges:
|
|
"""A class to store an admin's privileges."""
|
|
|
|
def __init__(self, privileges=[]):
|
|
self.privileges = privileges
|
|
|
|
def show_privileges(self):
|
|
print("\nPrivileges:")
|
|
if self.privileges:
|
|
for privilege in self.privileges:
|
|
print(f"- {privilege}")
|
|
else:
|
|
print("- This user has no privileges.")
|
|
|
|
|
|
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
|
|
eric.describe_user()
|
|
|
|
eric.privileges.show_privileges()
|
|
|
|
print("\nAdding privileges...")
|
|
eric_privileges = [
|
|
'can reset passwords',
|
|
'can moderate discussions',
|
|
'can suspend accounts',
|
|
]
|
|
eric.privileges.privileges = eric_privileges
|
|
eric.privileges.show_privileges() |