![]() |
TypeError: __init__() missing 1 required positional argument: 'successor - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: TypeError: __init__() missing 1 required positional argument: 'successor (/thread-32812.html) |
TypeError: __init__() missing 1 required positional argument: 'successor - siki - Mar-07-2021 Hi guys! I have a school project. The task is to print three presidents by class and objects using write(), also method __init__ to create presidents with name, country and inauguration date. I have already made a class President with three attributes: name, country and inauguration. That I have done. But the next task is to print the successor for each president: "Now you need to modify the program so that President objects also have a attribute successor that holds the object of the next president in the row. For the last president in line, the successor shall have the value None. The class should also have a method setSuccessor(next) that assigns the next president to successor. Example: clinton.Successor(bush) Now the president is represented by the variable presidents row contains the first president. The rest of the presidents are represented by successor." So this is what I have wrote so far, but I don't get it ![]() Programming Python is so hard, and the task we are given are so difficult I my opinion ... ![]() Appreciate all answers! # a) class President: def __init__(self, president, country, elected, successor): self.president = president self.country = country self.elected = elected self.successor = successor def write(self): print(self.president,"of the", self.country, "inauguration:", self.elected, self.successor) def __str__(self): return f"{self.president} of the {self.country}, inauguration: {self.elected} {self.successor}" clinton = President("Bill Clinton", "US", "1993") bush = President("George W Bush", "US", "2001") obama = President("Barack Obama", "US", "2009") print(clinton) print(bush) print(obama) presidents = [President("Bill Clinton", "US", "1993"), President("George W Bush", "US", "2001"), President("Barack Obama", "US", "2009")] # b) def setSuccessor(next): successor=next clinton.successor(bush) bush.sucessor(obama) obama.successor = None RE: TypeError: __init__() missing 1 required positional argument: 'successor - Larz60+ - Mar-08-2021 you set __init__ as follows: def __init__(self, president, country, elected, successor): this requires 4 parameters president, country, elected and successor if you wish to set a default value for successor, write this way: def __init__(self, president, country, elected, successor=None):
|