Python Forum

Full Version: Some Confusing Program Errors (Newbie stuff)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All!
So I am busy teaching myself Python and I am going through bits of code with supposed errors.
There is one particular one I am stuck with, that needs me to correct the class definitionin the code.... though I still have trouble making heads or tails of certain elements.
Could anyone please help?

class Car(object):
    numwheels = 4
    def display(self):
        print("Make:", self.make)
        print("Colour:", self.colour)
        print("Wheels:", self.numwheels)
#MainProgram
c0bj1 = Car("Ford","Black")
c0bj1.display()
Your class is missing constructor, which in Python is declared with __init__() method.

class Car(object):
    def __init__(self,make,colour):
        self.make = make
        self.colour = colour
        self.numwheels = 4

    def display(self):
        print("Make:", self.make)
        print("Colour:", self.colour)
        print("Wheels:", self.numwheels)

#MainProgram
c0bj1 = Car("Ford","Black")
c0bj1.display()