簡體   English   中英

Python:TypeError:__init__() 缺少 1 個必需的位置參數:'privileges' - 如何調試代碼?

[英]Python: TypeError: __init__() missing 1 required positional argument: 'privileges' - How do I debug the code?

class user():
    # This creates the two attributes called first_name and last_name and other attributes e.g. email and username
    def __init__(self, first_name, last_name, email, username ):
            self.first_name = first_name
            self.last_name = last_name
            self.email = email
            self.username = username
            self.login_attempts = 0
            

    # This method describe_user prints a summary of the user’s information. 
    def describe_user(self):
        print(f'Name: {self.first_name} {self.last_name}')
        print(f'Username: {self.username}')
        print(f'email: {self.email}')

    # This method greet user prints a personalized greeting to the user.
    def greet_user(self):
        print(f'Welcome back {self.first_name}')

class Admin(user):

    def __init__(self, first_name, last_name, email, username, privileges ):
        super().__init__(first_name, last_name, email, username)
        self.privileges = privileges

    # Creates method called show_privileges
    def show_privileges(self):
        print('Your admin privileges are:')
        for priv in self.privileges:
            print(f'      - {priv}')

privs = ['can add post', 'can delete post', 'can ban user']
gold = Admin('Peter', 'Gold', 'pgold@collin.edu', privs)


gold.show_privileges()
gold.describe_user()

創建 Admin 類的實例時缺少用戶名參數
gold = Admin('Peter', 'Gold', 'pgold@collin.edu', privs)應該是gold = Admin('Peter', 'Gold', 'pgold@collin.edu', 'username', privs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM