Home > Software engineering >  takes 1 positional argument but 2 were given error for dict
takes 1 positional argument but 2 were given error for dict

Time:12-22

how to add two dict in cs list

I want to add two dictionaries to the cs list in the addcoursess function, but it gives an error Text error: takes 1 positional argument but 2 were given

mainlist:

    cs = [
    {
        "title": "Python",
        "teacher": "Amiri",
    },
    {
        "title": "HTML",
        "teacher": "Dehyami",
    },
    {
        "title": "PHP",
        "teacher": "Enayati"
    }
]


class User:
    def __init__(self, fisrtname, lastname):
        self.fname = fisrtname
        self.lname = lastname

    def fullname(self):
        print(f"my fullname is {self.fname} {self.lname}")

class Student(User):
    def __init__(self, fisrtname, lastname, email):
        super().__init__(fisrtname, lastname)
        self.email = email
        self.coursess = []

    def fullname(self):
        print("i am an student")
        super().fullname()

    def printcoursess(self):
        if self.coursess:
            for course in self.coursess:
                print("Coursess : "   course["title"])
        else:
            print("There is no course")

Here is the class in which it is error

class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code

    def addcoursess(item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)
    def fullname(self):
        print("i am an teacher")
        super().fullname()


t1 = Teacher("abolfazl", "zaker", 3223)

addcoursess function here

t1.addcoursess({"title": "Java", "teacher": "ganjeali"})

print(cs)

CodePudding user response:

U miss the self argument in the function definition. Your Teacher class should look like this:

class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code

    def addcoursess(self, item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)

    def fullname(self):
        print("i am an teacher")
        super().fullname()

Or if u don't want to pass the self, the class should be defined with the @staticmethod decorator like this:

class Teacher(User):
        def __init__(self, fisrtname, lastname, code):
            super().__init__(fisrtname, lastname)
            self.code = code

        @staticmethod
        def addcoursess(item):
            dict = {}
            dict.update(item)
            cs.append(dict)
            print(dict)

        def fullname(self):
            print("i am an teacher")
            super().fullname()

Info about static method are here: https://docs.python.org/3/library/functions.html#staticmethod

CodePudding user response:

Your teacher class methods are missing self, keyword. It should look like below

class Teacher(User):
def __init__(self, fisrtname, lastname, code):
    super().__init__(fisrtname, lastname)
    self.code = code

def addcoursess(self, item):
    dict = {}
    dict.update(item)
    cs.append(dict)
    print(dict)

def fullname(self):
    print("i am an teacher")
    super().fullname()

CodePudding user response:

You forgot to add self in your addcoursess method of Teacher class, it should be addcourse(self,item):

Here is your complete working code.

cs = [
    {
        "title": "Python",
        "teacher": "Amiri",
    },
    {
        "title": "HTML",
        "teacher": "Dehyami",
    },
    {
        "title": "PHP",
        "teacher": "Enayati"
    }
]


class User:
    def __init__(self, fisrtname, lastname):
        self.fname = fisrtname
        self.lname = lastname

    def fullname(self):
        print(f"my fullname is {self.fname} {self.lname}")

class Student(User):
    def __init__(self, fisrtname, lastname, email):
        super().__init__(fisrtname, lastname)
        self.email = email
        self.coursess = []

    def fullname(self):
        print("i am an student")
        super().fullname()

    def printcoursess(self):
        if self.coursess:
            for course in self.coursess:
                print("Coursess : "   course["title"])
        else:
            print("There is no course")

class Teacher(User):
    def __init__(self, fisrtname, lastname, code):
        super().__init__(fisrtname, lastname)
        self.code = code

    def addcoursess(self, item):
        dict = {}
        dict.update(item)
        cs.append(dict)
        print(dict)
    def fullname(self):
        print("i am an teacher")
        super().fullname()


t1 = Teacher("abolfazl", "zaker", 3223)

t1.addcoursess({"title": "Java", "teacher": "ganjeali"})

print(cs)

CodePudding user response:

You have forgotten the self in your declaration

 def addcoursess(self, item):

The result by me is, as it follows :

[{'teacher': 'Amiri', 'title': 'Python'}, {'teacher': 'Dehyami', 'title': 'HTML'}, {'teacher': 'Enayati', 'title': 'PHP'}, {'teacher': 'ganjeali', 'title': 'Java'}]
  • Related