Python 3.x methods not starting out with a subtract/addition

I’m attempting utilizing TeamTreehouse learning subscription & this Starting Out With Programming Logic And Design (3rd Ed) book to attempt learning programming & Python. Please don’t shoot to kill me I’m learning.

Goal: I’m working to set a Vehicle class with 3 attributes; yearModel, make, speed which I need to later call a method 5 times to take action on the speed attribute. Speed attribute must have addition done 5 times on it then another method called to complete subtraction.

What Isn’t Happening: The first time I call the accelerate method the vehicle should be going 5 mph or that is my goal.

Research/Troubleshooting: I’ve been playing with doing a while loop/do while loop instead of the for loop though honestly I’m posting here without much as reference here. I feel I’m overlooking something because I’ve been working with this so much.

URL: http://repl.it/rQs/29

code:

#///////////Create Vehicle Object Template//////////////////////
# .self variables yearModel, make, speed

class Vehicle:
    __yearModel = int()
    __make = str("")
    __speed = int(0)

#///////////Object Constructor//////////////////////
# .self variables yearModel, make, speed

    def __init__(self, yearModel, make):
        self.__yearModel = yearModel
        self.__make = make
#///////////Object Constructor//////////////////////

#///////////METHODS aka functions in the object//////////////////////    

#///////////////MODEL//////////////
# .self variables yearModel, make, speed

    def setyearModel(self, yearModel):
        self.__yearModel = yearModel

    def getyearModel(self):
        return self.__yearModel

#///////////////MODEL//////////////        

#///////////////MAKE//////////////
# .self variables yearModel, make, speed

    def getmake(self):
        return self.__make

    def setmake(self, make):
        self.__make = make

#///////////////MODEL//////////////

#//////////////SPEED//////////////
# .Self variables yearModel, make, speed

    def setSpeed(self, speed):
        self.__speed = speed

    def getSpeed(self):
        return self.__speed
#//////////////SPEED//////////////

#//////////////ACCELERATE//////////////
# .self variables yearModel, make, speed
    def accelerate(self):
        self.__speed = self.__speed + 5
        if(self.__speed < 0):
            self.__speed = 0
#            print(self.speedy())

#        i = 0;
#        for i in range(5):
#            if (i <= 5):
#                self.__speed = self.__speed + 5
#                self.speedy()                
#            else:
#                break
#        i += 1
#//////////////SPEED//////////////

#//////////////BRAKE//////////////
# .self variables yearModel, make, speed

    def brake(self):

        self.__speed = self.__speed - 5
        if(self.__speed < 0):
            self.__speed = 0
#            print(self.speedy())

#        i = 0;
#        for i in range(5):
#            if (i <= 5):
#                self.__speed = self.__speed - 5
#                self.speedy()                
#            else:
#                break
#        i += 1

#    def brake(self):
#        self.__speed = self.__speed - 5
#        if(self.__speed < 0):
#            self.__speed = 0
#//////////////BRAKE//////////////

#//////////////PRINT//////////////
# .self variables yearModel, make, speed

    def speedy(self):
        print("The speed of the {} {} is currently {} MPH as it accelerates.".format(self.__yearModel,self.__make,self.__speed, end = ""))

    def speedyDown(self):
        print("The speed of the {} {} is currently {} MPH as it brakes.".format(self.__yearModel,self.__make,self.__speed, end = ""))

#//////////////PRINT//////////////

#///////////METHODS aka functions in the object^^^//////////////////////

#////////////////////// Object Initialization myVehicle///////////////

#////////////////////// User Inputs Section///////////////
def userInputs():
    yearModel=input("What is the year and model of the vehicle?n""Example: 1920 Modelt T.n")
    make=input("What automobile manufacturer made it?n")
    return yearModel, make

yearModel,make=userInputs()

#///////////Create Car Object Template^^^//////////////////////

myVehicle=Vehicle(yearModel,make)

#///////////Create Car Object Template^^^//////////////////////
print(myVehicle.speedy())
def speedAcceleratePrintout():
    i = 0;
    for i in range(5):
        if (i <= 5):
            myVehicle.accelerate()
            print(myVehicle.speedy())
#        elif (i>5 and i<-10):
#            myVehicle.brake()        
        else:
            break
    i += 1

def speedBrakePrintout():
    i = 0;
    for i in range(5):
        if (i <= 5):
            myVehicle.brake()
            print(myVehicle.speedyDown())
        else:
            break
    i += 1

speedAcceleratePrintout()
speedBrakePrintout()
#print(myVehicle.speedy())
#////////////////////// Object Initialization myCar^^^///////////////


Source: oop

Leave a Reply