jordanis1 2022-05-05 18:55 采纳率: 80%
浏览 71
已结题

请问这段代码错误的问题在哪?


class User:
    """模拟用户的一些参数"""
    def __init__(self,first_name,last_name,occupation):
        """初始化属性first_name,last_name和occupation"""
        self.first_name = first_name
        self.last_name = last_name
        self.occupation = occupation


    def describe_user(self):
        print(self.first_name,self.last_name,self.occupation)

    def greet_user(self):
        print(f"Hello,{self.first_name} {self.last_name}!")


"""9.8编写一个Privileges的类,引用9.7中的内容"""
class Privileges():
    def __int__(self):
        self.privileges=['can add post','can delete post','can ban user']

    def show_privileges(self):
        print(f"The admin's privileges are {self.privileges}")


"""编写一个继承User类的名为Admin的子类"""
class Admin(User):
    def __init__(self,first_name,last_name,occupation):
        super().__init__(first_name,last_name,occupation)
        self.privileges=Privileges()


lee = Admin('biee','lee','Engineer')
lee.describe_user()
lee.greet_user()
lee.privileges.show_privileges()

错误信息:

biee lee Engineer
Hello,biee lee!
Traceback (most recent call last):
  File "C:\Users\Lee\Desktop\python_work\Chapter 9\privileges.py", line 36, in <module>
    lee.privileges.show_privileges()
  File "C:\Users\Lee\Desktop\python_work\Chapter 9\privileges.py", line 23, in show_privileges
    print(f"The admin's privileges are {self.privileges}")
AttributeError: 'Privileges' object has no attribute 'privileges'
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "C:\Users\Lee\Desktop\python_work\Chapter 9\privileges.py"]
[dir: C:\Users\Lee\Desktop\python_work\Chapter 9]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Users\Lee\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\Lee\AppData\Local\Programs\Python\Python310\;C:\Users\Lee\AppData\Local\Microsoft\WindowsApps;]

请问错在哪里,查了好久。

  • 写回答

5条回答 默认 最新

  • keenanli 2022-05-05 19:02
    关注
    __int__
    

    第20行单词拼错了
    应该是

    __init__
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 5月13日
  • 已采纳回答 5月5日
  • 创建了问题 5月5日

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题