mardi 18 avril 2017

Polymorphism and Classes in Python

I am having trouble completeing this assignment for school.

Here is the problem I am tasked with:

"In the main.py file create 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 cylinder, 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 typ and drive type."

Here is my classes file named vehciles

#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):
        Automobile.__init__(self)

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

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


class Truck(Automobile):

    def __init__(self):
        print('Truck')

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

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


class Hatchback(Automobile):

    def __init__(self):
        print('Hatchback')

    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






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire