class User():
def __init__(self,first,last,sex,age):
self.first_name=first
self.last_name=last
self.sex = sex
self.age =age
self.login_attempts = 0
def describe_users(self):
print(self.first_name + " " +self.last_name)
print("Gender: " + self.sex)
print("Age: " + str(self.age))
def greeting_users(self):
print("Welcome, " + self.first_name + " " + self.last_name + "!\n")
def increment_login_attempts(self):
self.login_attempts += 1
print("Login attempts: " + str(self.login_attempts))
def reset_login_attempts(self):
self.login_attempts = 0
print("Login has been reset")
class Privileges():
def __init__(self,privileges):
self.privileges= ['can add post','can delete post', 'can ban user']
def show_privileges(self):
for x in self.privileges:
print("The Admin can " + x)
class Admin(User):
def __init__(self,first,last,sex,age):
"""Initialize attributes of the parent class"""
super().__init__(first,last,sex,age)
self.privileges = Privileges()
person_admin= Admin('Kevin','Mark','M',30)
person_admin.Privileges.show_privileges()
所以我无法解决这个问题。我继续得到错误Typeerror。想知道你们是否有人知道问题出在哪里?此问题的目标是将“权限”实例作为“Admin”类中的属性。之后,创建一个新的“Admin”实例并使用“show_privileges”来显示其权限。
答案 0 :(得分:1)
确切的错误可能是这样的:
Traceback (most recent call last):
File "python", line 42, in <module>
File "python", line 40, in __init__
TypeError: __init__() missing 1 required positional argument: 'privileges'
这是因为你编写了Privileges的构造函数来要求一个参数,但是在Admin的构造函数中实例化Privileges时你还没有传递那个参数。只需将Privileges的构造函数更改为:
def __init__(self): # removed argument "privileges"
self.privileges = ['can add post','can delete post', 'can ban user']
然后在最后一行你会想要小写权限,因为你指的是实例而不是类。
person_admin.privileges.show_privileges()
您可以查看working code here。