StrawberryZz 2023-02-25 22:33 采纳率: 100%
浏览 31
已结题

NameError: name 'privileges' is not defined. Did you mean: 'privilege'?

不知道是哪里出问题了。

报错如下:


Privileges:
- This user has no privileges.

Adding privileges...

Privileges:
Traceback (most recent call last):
  File "C:\Users\82791\Desktop\python_work\第9章\privileges.py", line 49, in <module>
    eric.privileges.show_privileges() 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\82791\Desktop\python_work\第9章\privileges.py", line 33, in show_privileges
    for privilege in privileges:
                     ^^^^^^^^^^
NameError: name 'privileges' is not defined. Did you mean: 'privilege'?

代码如下:

class User:
    """一个表示用户的简单类。"""

    def __init__(self, first_name, last_name, username, email, location):
        """初始化用户。"""
        self.first_name = first_name.title()
        self.last_name = last_name.title()
        self.username = username
        self.email = email
        self.location = location.title()

class Admin(User):
    """有管理权限的用户。"""

    def __init__(self, first_name, last_name, username, email, location):
        """初始化管理员。"""
        super().__init__(first_name, last_name, username, email, location)

        # 将权限集初始化为空。
        self.privileges = Privileges()


class Privileges():
    """一个存储管理员权限的类。"""

    def __init__(self, privileges=[]):
        self.privileges = privileges

    def show_privileges(self):
        print("\nPrivileges:")
        if self.privileges:
            for privilege in privileges:
                print(f"- {privilege}")
        else:
            print("- This user has no privileges.")
    
eric = Admin('eric', 'matthes', 'e_matthes', 'e_matthes@example.com', 'alaska')

eric.privileges.show_privileges()

print("\nAdding privileges...")
eric_privileges = [
    'can reset passwords',
    'can moderate discussions',
    'can suspend accounts',
    ] 
eric.privileges.privileges = eric_privileges
eric.privileges.show_privileges() 


  • 写回答

2条回答 默认 最新

  • ALittleHigh 2023-02-25 22:41
    关注

    应该是self.previleges

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

报告相同问题?

问题事件

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

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵