Traceback (most recent call last):
File "C:/Users/szy/Desktop/python例子/9-3.py", line 24, in
user_1.describe_user()
TypeError: describe_user() takes no arguments (1 given)
代码:
class User(object):
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():
full_name = self.first_name +self.last_name
print("\nThe user's full name is " + full_name.title() + '.')
print("The user is " + str(self.age) + "years old.")
print("The user is a " + self.gender + ".")
def greet_user():
if self.gender == 'man':
print("Hello, Mr " + self.first_name + '!' )
else:
print("Hello,Mrs " + self.first_name + '!')
print(full_name.title())
user1 = User('smith','tom',34,'men')
user2 = User('Elizabeth','alice',24,'women')
user1.describe_user()
user1.greet_user()
user2.describe_user()
user2.greet_user()