I read a lot of car classes on several sites and still can't get mine to work. Any ideas?

##Car class

class Car:
    def __init__(self, y_model, make, speed):
        self.y_model = y_model
        self.make = make
        self.speed = speed

    def set_year_model(self, y_model):
        self.y_model = y_model
    def set_make(self, make):
        self.make = make
    

    def get_year_mod(self):
        return self.y_model
    def get_make(self):
        return self.make

    def accelerate(self, speed):
        self.speed += 5
        
    def brake(self, speed):
        self.speed -= 5
            
    def get_speed(self):
        return self.speed
##Car

import car


def main():
    my_car = car.Car(mod, ekam, speed)
 
    mod = raw_input('What year is your car')

    ekam = raw_input('What is the make of your car')

    print my_car.get_year_mod()

    print my_car.get_make()

    speed = 0

    print my_car.accelerate(speed)

    print my_car.brake(speed)

main()

Edited 6 Years Ago by Ghostenshell: n/a

Couple of problems. The first is poor class implementation.

It's OK to make your class without attribute getters and setters. You can access instance attributes by referring to the attributes themselves.

class Car:

    def __init__(self, year):
        self.year = year

mycar = Car(1999)
mycar.year

The second problem with implementation is that you require a speed argument to accelerate and decelerate the car, but inside of those methods you accelerate or decelerate by a constant of 5. Is that desired behavior, or would you rather to accelerate or decelerate by the amount passed as the argument?

The third problem is with your script's structure. You try to create the car before you've gotten the user's input. You must assign identifiers before you can use them elsewhere.

year = raw_input('What year is your car? ')
mycar = car.Car(year)
print mycar.year

You did a decent job with the class for a beginner. Don't get discouraged. Good luck.

Shouldn't you ask parameters before creating the car, not after? Why the getters and setters? It is concidered unpythonic. Use mycar.mod = instead of mycar.set_yearmod() and mycar.mod instead of the getter.

This is the structure the professor wants it in. We are still using python 2.6. Thanks for the help. Going to go try some more.

Here's my final code. The comments are intended for my instructor.

##Car class

class Car:
    ##Methods
    def __init__(self, y_model, make, speed):
        self.y_model = y_model
        self.make = make
        self.speed = speed+5
    ##Setters and getters
    def set_year_model(self, y_model):
        self.y_model = y_model
    def set_make(self, make):
        self.make = make
    

    def get_year_mod(self):
        return self.y_model
    def get_make(self):
        return self.make
    ##Math is there an easier way?   
    def accelerate(self, speed):
        ##speed += 5  Couldn't figure out how to work this in the problem.
        accel = speed + 5
        return accel
        
    def brake(self, speed):
        ekarb = speed - 5
        return ekarb
    ##def get_speed(self, speed):
          #return speed
    
def main():
    
    ##Tried this in a different file but didn't work.
    #my_car = Car()

    ##Get user input
    mod = raw_input('What year is your car: ')

    ekam = raw_input('What is the make of your car: ')
    ##set speed
    speed = 5
    ##Is this creating the object?##  #Same as my_car = Car()#
    my_car = Car(mod, ekam, speed)
    
    ##Call Accel and deccel Model and Make
    ## Should year_mod be input instead of raw_input?
    print my_car.get_year_mod()

    print my_car.get_make()

    print 'Your car broke down, hold on I will push'
    print 'You are going', speed,'mph'
    print 'This hill is steep!!!'

    while my_car.accelerate(speed) !=40:
        speed = my_car.accelerate(speed)
        print speed, 'mph'
    

    while my_car.brake(speed) !=0:
        speed = my_car.brake(speed)
        print speed, 'mph'
main()
This question has already been answered. Start a new discussion instead.