0

I've been having issues getting started with classes. I'm getting this Type Error, but I'm not sure what's been causing it. I checked other questions, but their answers don't seem to be the solution to my problem.

#create the car class
class Car:

    #create the initiator function
    def __init__carStats(self, year, model):
        self.__speed = 0
        self.__year = year
        self.__model = model

    def setModel(self, model):
        self.__model = model

    def setYear(self, year):
        self.__year = year

    #create speed increase function
    def speedUp(self):
        print('Calling speed up function.')
        self.__speed += 5

    #create slowdown function
    def slowDown(self):
        print('Calling slow down function.')
        self.__speed -= 5

    #create show speed function
    def showSpeed(self):
        print('The', self.__year, self.__model, '\'s current speed is', self.__speed,'miles per houer')

#create main function
def Main():
    year = ''
    make = ''
    #get car year and make
    make = input('What model is the car? \n')
    year = input('What year is the car?" \n')

    myCar = Car(year, make)

Main()

1 Answers1

0

The "constructor" of Python classes has to be called __init__ and the default __init__ does not take parameters thus you get the type error. Change your init to:

def __init__(self, year, model):

There is no way of overloading __init__, but you could use named arguments with defaults so they can be omitted.

def __init__(self, year=None, model=None):

See What is a clean, pythonic way to have multiple constructors in Python?

Community
  • 1
  • 1
wedi
  • 1,332
  • 1
  • 13
  • 28