Python 添加子类后,代码不起作用。有人能指出原因吗?
这是我的密码。这在儿童课之前就奏效了。但现在我得到了以下回溯Python 添加子类后,代码不起作用。有人能指出原因吗?,python,Python,这是我的密码。这在儿童课之前就奏效了。但现在我得到了以下回溯 class User: """uses the user database for printing their info and greetings""" def __init__(self, first_name, last_name, age, joined_date, sex): """set variables for the user info""" self.first
class User:
"""uses the user database for printing their info and greetings"""
def __init__(self, first_name, last_name, age, joined_date, sex):
"""set variables for the user info"""
self.first_name = first_name
self.last_name = last_name
self.full_name = f'{first_name} {last_name}'
self.age = age
self.joined_date = joined_date
self.sex = sex
def describe_user(self):
"""prints info on the user"""
prompt = f"{self.full_name.title()}, a {self.sex} of age {self.age}"
prompt += f" years. Joined on {self.joined_date}."
print(prompt)
def greet_user(self):
"""greetings"""
print(f"Hello, {self.first_name.title()}, how are you doing?")
class Admin(User):
"""subclass for admin"""
def __init__(self, first_name, last_name, age, joined_date, sex,):
"""initialise attriibutes of admin"""
super.__init__(first_name, last_name, age, joined_date, sex)
self.can_ban_user = True
self.can_delete_post = True
self.can_add_post = True
def show_privileges(self, show):
prompt = ['']
if self.can_add_post == True:
prompt += (f"User can add posts, ")
if self.can_ban_user == True:
prompt += (f"User can delete posts")
if self.can_ban_user == True:
prompt +=(f"User can ban other users")
print(prompt)
me = Admin('l', 'na', 21, '19-06-2020', 'male')
me.show_privileges()
回溯(最近一次呼叫最后一次):
文件“C:\Users\$$\Desktop\python\u work\9-7.py”,第44行,在
me=管理员('l','na',21',19-06-2020','male')
文件“C:\Users\$$\Desktop\python\u work\9-7.py”,第28行,在\u\u init中__
super.\uuuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
TypeError:描述符“\uuuu init\uuuuuu”需要一个“super”对象,但收到一个“str”
我运行了您的代码,但有几个问题:
- 首先,super()函数应该有两个参数:
super(Admin, self).__init__(first_name, last_name, age, joined_date, sex)
- 其次,函数
有一个参数show_privileges()
,您没有使用它show
- 第三,您的输出以dict形式返回
干杯 我运行了你的代码,但有几个问题:
- 首先,super()函数应该有两个参数:
super(Admin, self).__init__(first_name, last_name, age, joined_date, sex)
- 其次,函数
有一个参数show_privileges()
,您没有使用它show
- 第三,您的输出以dict形式返回
干杯 “这是我的密码”不是问题。到底是什么问题?如果有错误消息,我们需要查看完整的回溯。如果代码的行为不符合预期,我们需要知道它的实际行为,以及您的预期。您所说的“不工作”是什么意思?请澄清这个问题。请参阅以获取参考,如果需要更多建议,请参阅。您缺少调用
super
的括号。应该是super()。\uuuu init\uuuu(…
“这是我的代码”不是问题。到底是什么问题?如果有错误消息,我们需要查看完整的回溯。如果代码没有按预期运行,我们需要知道它实际运行的是什么,以及您预期的是什么。您所说的“不工作”是什么意思?请澄清问题。请参阅以获取参考,如果您需要更多建议,请参阅。您缺少调用super
的括号。它应该是super()函数应该有两个参数:
在Python3中不需要。您现在可以不带参数调用super
。但是,是的,它确实需要被调用才能实际工作。而且它是一个类,而不是一个函数。首先,super()函数应该有两个参数:
在Python 3中不需要。您现在可以不带参数调用super
。但是,是的,它确实需要被调用才能实际工作。而且它是一个类,而不是一个函数。