2

enter image description hereI am having trouble completing this assignment for school. This picture is what the output should be when complete.

Here is the problem I am tasked with:

"In the main.py file creates four objects. (vehicle, sedan, truck, hatchback) The main.py should have a main function and a function to display the contents of each object. It should also use the isinstance function to test for valid data (Tennis shoes is not a vehicle!) In the vehicle.py file create a superclass called Automobile and three subclasses (sedan, truck, and hatchback) The Automobile class should have three attributes (make,engine_type and drive_type. Figure 2 shows the values for these attributes as Basic Sedan, 6 cylinders, Front Wheel. Sedan subclass should inherit the Automobile class and override the engine type. Truck and hatchback subclasses should inherit the Automobile class and override both the engine type and drive type."

Here is my classes file named vehicles

    #create a superclass named Automobile
class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    def show_make(self):
        print('Automobile Make:', self.__make)

    def show_drive_type(self):
        print(self.__drive_type)

    def show_engine_type(self):
        print(self.__engine_type)



class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('4 cylinder')

    def drive_type(self):
        Automobile.__init__(self)


class Truck(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('8 cylinder')

    def drive_type(self):
        print('Four Wheel')


class Hatchback(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        print('Electric')

    def drive_type(self):
        print('All Wheel')

And here is my main file with my main function inside

#import the vehicles file with classes
import vehicles

#define the main function
def main():
    #create an Automobile object, and all subclass objects
    automobile = vehicles.Automobile('Sedan', '6 Cylinder', 'Front Wheel')
    sedan = vehicles.Sedan()
    truck = vehicles.Truck()
    hatchback = vehicles.Hatchback()



    #display information about each one
    print('Here are some vehicles and their engine types.')
    print()
    show_auto_info(automobile)
    print()
    show_auto_info(sedan)
    print()
    show_auto_info(truck)
    print()
    show_auto_info(hatchback)
    print()
    show_auto_info('Tennis Shoes')

#define the show_auto_info function
def show_auto_info(car):
    if isinstance(car, vehicles.Automobile):
        car.show_make()
        car.show_drive_type()
        car.show_engine_type()
    else:
        print(car, 'is not a vehicle!')



main()

This is the error that i'm getting: Automobile.__init__(self) TypeError: __init__() missing 3 required positional arguments: 'make', 'engine_type', and 'drive_type'

Process finished with exit code 1

Classicalclown
  • 75
  • 1
  • 2
  • 10

3 Answers3

0

The problem is that your subclass does not have the same signature as your parent class, so considering Automobile class is this:

class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    # rest of the class

Your child class let's say Sedan should be:

class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        super().__init__(make, engine_type, drive_type)

However, if you want to make this code compatible with Python 2 and 3 you could use:

class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

If you want more information about extending a class in Python you can check this.

Update:

When creating an object of your subclass, use this

sedan = vehicles.Sedan('make_value', 'engine_type_value', 'drive_type_value')
Community
  • 1
  • 1
lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228
  • Oh okay, so each class needs to have the exact same arguments as the superclass? Will this be the same if i need to overwrite certain aspects of the superclass with each subclass such as a different engine_type etc? – Classicalclown Apr 18 '17 at 20:41
  • @Classicalclown, at least the same arguments in the superclass, you can add more arguments that are specific to your subclass. – lmiguelvargasf Apr 18 '17 at 20:42
  • I went ahead and did what you said but I'm still getting an error – Classicalclown Apr 18 '17 at 20:53
  • The same as before. I updated my code and when I run the main function, it still does the same thing. I also provided a picture of what the output should be. – Classicalclown Apr 18 '17 at 21:09
  • @Clasicalclown, I have updated my answer, your problem is that when you create an object of a child class you are not passing all the parameters. – lmiguelvargasf Apr 18 '17 at 21:11
  • Your answer gets me the output that I need but not the way that I need it. I'm not supposed to put the arguments in the main file, they are supposed to come from the vehicles file and overwrite the superclass when there is something different. Like the Sedan class is supposed to overwrite the cylinders and etc – Classicalclown Apr 18 '17 at 21:19
  • @Classicalclown, I think you are not understanding well your task, or at least you are not explaining it well here. – lmiguelvargasf Apr 18 '17 at 21:35
  • Basically, I need to have a file named vehicles that has a superclass named Automobile and subclasses that are named sedan, truck, and hatchback. The baseclass will be called and and show the basic sedan. Then the subclass of sedan will be called and it will use the same information from the baseclass but overwrite the engine_type with 4 cylinder. The truck and hatchback classes will overwrite everything with their own info – Classicalclown Apr 18 '17 at 21:38
  • @Classicalclown, the problem is that you cannot omit parameters of your superclass in your subclass. It is like you have the Animal class which has age and color, and you want to omit age in your subclass Dog, that is not possible. – lmiguelvargasf Apr 18 '17 at 21:48
0

Okey... you are a little lost here... your code brokes, but it will be broken again, so I will be some steps ahead:

#create a superclass named Automobile
class Automobile:

    #the __init__method accepts an argument for the automobiles make

    def __init__(self, make, engine_type, drive_type):
        self.__make = make
        self.__engine_type = engine_type
        self.__drive_type = drive_type

    def show_make(self):
        print('Automobile Make:', self.__make)

    def show_drive_type(self):
        print(self.__drive_type)

    def show_engine_type(self):
        print(self.__engine_type)



class Sedan(Automobile):

    def __init__(self, make, engine_type, drive_type):
        Automobile.__init__(self, make, engine_type, drive_type)

    def engine_type(self):
        self.__engine_type = '4 cylinder'
        return self.__engine_type

    def drive_type(self):
        return self.drive_type


class Truck(Automobile):

    def __init__(self):
        Automobile.__init__(self, 'Truck', None, None)

    def engine_type(self):
        self.__engine_type = '8 cylinder'
        return self.__engine_type

    def drive_type(self):
        self.__drive_type = 'Four Wheel'
        return self.__drive_type


class Hatchback(Automobile):
... same here 

Notice that I put some return inside the methods, that's because you latter must do some extra method that print you the car description.

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

This is how the Main Method should look like, you must end the rest of the code by yourself with this, if you don't understand my code, write me again and I will explain to you

Main Class:

#import the vehicles file with classes
import vehicles

#define the main function
def main():
    #create an Automobile object, and all subclass objects
    automobile = vehicles.Automobile('Sedan', '6 Cylinder', 'Front Wheel')
    sedan = vehicles.Sedan('Sedan', '24 Cylinder', 'amaizing car')
    truck = vehicles.Truck()



    #display information about each one
    print('Here are some vehicles and their engine types.')
    print()
    show_auto_info(automobile)
    print()
    show_auto_info(sedan)
    print()
    show_auto_info(truck)
    show_auto_info('Tennis Shoes')

#define the show_auto_info function
def show_auto_info(car):
    if isinstance(car, vehicles.Automobile):
        car.show_make()
        car.show_drive_type()
        car.show_engine_type()
    else:
        print(car, 'is not a vehicle!')



main()
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
  • The only problem with your code is that you aren't overwriting the basic sedan superclass, you're just passing arguments for different attributes into the main function when I need to just pass the arguments that are different for each type of vehicle – Classicalclown Apr 19 '17 at 21:13
  • Mmmm, I'm not follow, could you write for me please what's you wished String output after the main() execution? What shoul it be? Maybe I didn't understand the ptoblem – developer_hatch Apr 20 '17 at 13:20