简体   繁体   中英

TypeError: __init__() missing 1 required positional argument: 'successor

I have a school project. The task is to print three presidents by class and objects. 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:( I get this error I wrote in subject. Missing argument successor. But my guess is that thats not only the problem.

# 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

As you can see I have tried to implement successor, but Im not sure if that is correct.

Appreciate all help, thank you so much in advance!

The error that you see is because the __init__() method of the President class requires an argument for successor, but you don't supply it when creating presidents.

setSuccessor(next) needs to be a method of class President . Your code declares it as a normal function. Also, don't use next as a variable name as that overrides the builtin next() function:

Also the __init__() no longer requires the successor argument, or preferably you could make it's default value None .

class President:

    def __init__(self, president, country, elected, successor=None):
        self.president = president
        self.country = country
        self.elected = elected
        self.successor = successor

    def set_successor(self, successor):
        self.successor = successor


biden = President("Joe Biden", "US", "2021")    # no successor argument supplied - this president has no successor yet
trump = President("Donald Trump", "US", "2017", biden)

harris = President("Kamala Harris", "US", "2021")
biden.set_successor(harris)    # a bold prediction

You might like to add a method to update the inauguration year if, for example, Trump were again to become president.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM