0

I am attempting to create a Linked list of appointments, but when I run test code it gives me a type error saying that I am missing 3 positional arguments. I would imagine the solution is something simple but I've been tinkering with it for awhile and cannot figure it out.

from datetime import datetime


class VaccList:
    class Appointment:
        def __init__(self, name, age, city, date):
            assert type(name) is str, 'name variable must be a string'
            assert type(age) is int, 'age variable must be a integer'
            assert type(city) is str, 'city variable must be a string'
            assert type(date) is datetime, 'date variable must be a datetime object'
            assert name != None, 'name variable cannot be empty'
            assert age >= 18 and age <= 100, 'age must be between 18 and 100'
            #   ADD 6 asserts.  4 for the types and name cannot be empty, 
            #   and age must be between 18 and 100

        self.name = name
        self.age = age
        self.city = city
        self.date = date
        self.confirmed = False
        self.next = None

        def __str__(self):
            s = "Appointment for " + self.name + " on " + str(self.date) + " age:" + str(
            self.age) + "  city:" + self.city
            if self.confirmed:
                s += " (confirmed)"
            else:
                s += " (unconfirmed)"
            return s

    def __init__(self):
        self.head = None
        self.tail = None

    def isEmpty(self):
        return self.head is None

    def print(self):  
        '''
        Print all the appointments, one per line.  Print a blank line after the last one.
        If the list is empty, print a line saying the Appointment List is empty.
        '''
        pass

    def append(self, newAppt):  
        '''  Given a pointer to an Appointment object, tack it onto the end.
            If the list was empty, then make sure to set both pointers! 
            This is only used for appointments that are canceled or completed, 
            since we want the active list to remain sorted by date.
        '''
        assert type(newAppt) is VaccList.Appointment, "append() requires a pointer to an Appointment object."
        #  Note, no loop is needed!
        newnode = VaccList.Appointment(newAppt)
        if self.head is None:
            self.head = newnode
        else:
            newnode.next = self.head
            self.head = newnode

if __name__ == "__main__":

    active = VaccList()
    appt = VaccList.Appointment("Henry", 72, "Buffalo", datetime(2021, 5, 1, 12, 0, 0))
    active.append(appt)

Below is the following error I keep encountering:

Traceback (most recent call last):
  File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 152, in <module>
active.append(appt)
  File "/Users/nicholas/Downloads/SHELL/vacclist.py", line 54, in append
    newnode = VaccList.Appointment(newAppt)
TypeError: __init__() missing 3 required positional arguments: 'age', 'city', and 'date'

Any help on this is truly appreciated as I am in the beginning stages of learning python.

user52601
  • 27
  • 5
  • 2
    `Appointment`'s `__init__` method takes _four_ arguments (excluding `self`), but you only pass it one (`newAppt`). You need to specify `name`, `age`, `city`, and `date`. – crcvd Mar 16 '21 at 20:28
  • One small recommendation to the code you have written: don't write the definition of one class inside another, it's hard to read. Define the class outside and you can use it inside as a composition. – the23Effect Mar 16 '21 at 20:36
  • If you give me permission, I can edit this code to show how to change that. Check this post out for more information on when you can use it: https://stackoverflow.com/questions/78799/is-there-a-benefit-to-defining-a-class-inside-another-class-in-python – the23Effect Mar 16 '21 at 20:40
  • @the23Effect Unfortunately my professor gave me some code already written and he had the Appointment class within the VaccList class. I do not prefer this method but I think that is how he wants it handed in. – user52601 Mar 16 '21 at 20:43
  • Okay, that is totally understandable, besides this was just a recommendation. – the23Effect Mar 16 '21 at 20:44
  • Okay so the comment by @crcvd should help you out with the answer. Let me know if you still didn't figure out. – the23Effect Mar 16 '21 at 20:51
  • And please do mark this as closed if you have solved it. Have fun learning Python! :) – the23Effect Mar 16 '21 at 20:53

0 Answers0