Practise:
Make a class called User. Create two attributes called first_name and last_name, and then create several other attributes that are typically stored in a user profile. Make a method calleddescribe_user() that prints a summary of the user’s information. Make another method calledgreet_user() that prints a personalized greeting to the user.
My answer
class User():
'''build a class named user'''
def _init_(self, first_name, last_name, age, gender):
self.first_name = first_name
self.last_name = last_name
self.age = age
self.gender = gender
def describe_user(self):
'''print the user's info'''
print("Your first name is: " + self.first_name.title() + ".")
print("Your last name is: " + self.last_name.title() + ".")
print("Your age is: " + self.age.title() + ".")
print("Your gender is: " + self.gender.title() + ".")
def greet_user(self):
'''say hello to the user'''
print("Hello, " + self.first_name.title() + " " + self.last_name.title() + ", nice to see you here. Hope you have a good time today!")
user_one = User("Ribon", "Woo", "18", "female")
user_one.describe_user()
user_one.greet_user()
user_two = User("Jack", "Jones", "23", "male")
user_two.describe_user()
user_two.greet_user()
user_three = User("Rebecca", "Cai", "98", "female")
user_three.describe_user()
user_three.greet_user()
user_four = User("Tina", "Faye", "56", "female")
user_four.describe_user()
user_four.greet_user()
and the textbook’s answer:
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()
def describe_user(self):
"""Display a summary of the user's information."""
print("\n" + self.first_name + " " + self.last_name)
print(" Username: " + self.username)
print(" Email: " + self.email)
print(" Location: " + self.location)
def greet_user(self):
"""Display a personalized greeting to the user."""
print("\nWelcome back, " + self.username + "!")
eric = User('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')
eric.describe_user()
eric.greet_user()
willie = User('willie', 'burger', 'willieburger', 'wb@example.com', 'alaska')
willie.describe_user()
willie.greet_user()
I thinke I followed the right steps, but get this error code:
TypeError
User() takes no arguments
File "F:\Ruby\PYTHON\Python Crash Course\PCC_9\PCC_9_3.py", line 24, in <module>
user_one = User("Ribon", "Woo", "18", "female")
Confused.