Hi there i have completed a an exercise from a book and wanted you guys to have a look, I think its giving me back the results needed but i cant tell for sure if i have answered the questions rightly and the code i wrote is not error prone.
better coding suggestions are welcome:
“”"
User Class:
Test 1:
1 Make a class called User.
2 Create two attributes called first_name and last_name
3 Create several other attributes that are typically
stored in a user profile.
4 Make a method called describe_user() that prints a summary
of the user’s information.
5 Make another method called greet_user() that prints a personalized greeting to the user.
6 Create several instances representing different users, and call both methods for each user
“”"
class User:
def __init__(self, firstname, lastname, age, education, married, employed):
self.firstname = firstname
self.lastname = lastname
self.age = age
self.education = education
self.married = married
self.employed = employed
self.login_attempts = 0
def describe_user(self):
return f'Profile: {self.firstname} {self.lastname}, {self.age} years old, a {self.education}, fully {self.married}, and {self.employed}'
def greet_user(self):
# Print a personalized message for your user
return f"Hi {self.firstname} you are welcome:"
# Instances created for many people
profile = User('Amin', 'Umar', 47, 'Developer', 'Married', 'Employed')
gyasi = User('Gyasi', 'Kobina', 52, 'Boga', 'Married', 'Employed')
akos = User('Maa', 'Akos', 47, 'Trader', 'Married', 'Employed')
kyei = User('Kyei', 'Mensa', 25, 'boys boys', 'Married', 'Employed')
# Calls
gyasi.describe_user()
gyasi.greet_user()
“”"
Please note, am new and want to know if i was able to answer the test questions well, and if the coding is logical:
i will be glad if anyone can help with another code tackling the test their own way, seeing another code will help me a lot. Thank you all in advance
Test 2
Login Attempts:
1 Add an attribute called login_attempts to your User class from the previous exercise.
2 Write a method called increment_login_attempts() that increments the value of login_attempts by 1.
3 Write another method called reset_login_attempts() that resets the value of login_attempts to 0.
4 Make an instance of the User class and call increment_login_attempts() several times.
5 Print the value of login_attempts to make sure it was incremented properly, and then call reset_login_attempts().
Print login_attempts again to make sure it was reset to 0
“”"
def increment_login_attempt(self, attempt):
# A Method that increments the value of login_attempts by 1
self.login_attempts += attempt
def reset_login_attempts(self):
# A method to reset login_attempts to 0
self.login_attempts -= self.login_attempts
print(f"login attempts is reset to: {self.login_attempts}")
# instance
gyasi.login_attempts = 2 # modified login from original value.
gyasi.increment_login_attempt(1) # Increment of loging attempts.
print(gyasi.login_attempts) # printing and expecting 3 as attempts .
# an attempt to display the reset user login_attempt to 0:
gyasi.login_attempts = 5
gyasi.reset_login_attempts()