Python Object-Oriented Python Instant Objects Master Class

Unashe Mutambashora
Unashe Mutambashora
2,483 Points

increment of number of laps

Any ideas on how I can increase the laps every time the method run_lap is called?

racecar.py
class RaceCar:



     def __init__(self,color,fuel_remaining,laps = 0,**Kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps

        for attribute,value in Kwargs.items():
            setattr(self,attribute,value)

    def run_lap(self.length):
        laps = laps + 1
        return fuel_remaining - (length * 0.0125)

3 Answers

You have a few problems with your code.

class RaceCar:



     def __init__(self,color,fuel_remaining,laps = 0,**Kwargs):  # 👈 An extra indent (error)
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps

        for attribute,value in Kwargs.items():
            setattr(self,attribute,value)

    def run_lap(self.length):  # 👈 Used a period (error)
        laps = laps + 1  # 👈 Incorrect method to increment laps try (self.laps += 1)
        return fuel_remaining - (length * 0.0125)  # 👈 Incorrect method to use fuel_remaining. Also additional 0 in place
                                                  #    Try (self.fuel_remaining -= length * 0.125)

Try to make these changes, and you are golden.

If problems still persists, tag me again by leaving a comment, and I will try to help out.

Unashe Mutambashora
Unashe Mutambashora
2,483 Points

Thanks for your response mate. I tried making those adjustments and still I am getting it wrong

class RaceCar: laps = 0

def __init__ (self,color,fuel_remaining,**kwargs):
    self.color = color
    self.fuel_remaining = fuel_remaining


    for attribute,value in kwargs.items():
        setattr(self,attribute,value)

def run_laps(self,length):
    self.laps+=1
    return self.fuel_remaining-=length * 0.125
class RaceCar:

    def __init__ (self,color,fuel_remaining,**kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = 0


        for attribute,value in kwargs.items():
            setattr(self,attribute,value)

    def run_laps(self,length):
        self.laps+=1
        self.fuel_remaining -= length * 0.125  👈 # no need to use return 
Marion Lackie
Marion Lackie
1,620 Points

Hi boi, this was really helpful. What I don't quite understand is why laps isn't set as an argument in the first line of the def_init_ method, along with the color and fuel_remaining arguments. I suspect this highlights my incomplete understanding of creating methods!!

The self.laps argument should not be altered with, the self.laps argument is a starting value and should be away from the user, suppose if we have set the self.laps argument 👇

class RaceCar:

    def __init__ (self, color, fuel_remaining, laps = 0, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = laps


        for attribute,value in kwargs.items():
            setattr(self,attribute,value)


    def run_laps(self,length):
        self.laps+=1
        self.fuel_remaining -= length * 0.125

Now the self.laps argument can be set to anything, it would make no sense because the self.laps attribute is like a condition, it will increase by default only if the run_laps method is used.

Some attributes should be set behind the scenes, so it can be default or constant, that value should not be changed.

Note: In the above example even if the self.laps argument is not given, it still can be changed by the user 👇

 class RaceCar:

    def __init__ (self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining
        self.laps = 0


        for attribute,value in kwargs.items():
            setattr(self,attribute,value)


    def run_laps(self,length):
        self.laps+=1
        self.fuel_remaining -= length * 0.125

>>> car = RaceCar("blue", 45, laps = 100)

>>> car.laps
100

This is because of the for_loop setattr, to avoid this make these changes 👇

class RaceCar:

    def __init__ (self, color, fuel_remaining, **kwargs):
        self.color = color
        self.fuel_remaining = fuel_remaining

        for attribute,value in kwargs.items():
            setattr(self,attribute,value)

        self.laps = 0 👈 #Set (self.laps) after the for loop

    def run_laps(self,length):
        self.laps+=1
        self.fuel_remaining -= length * 0.125

>>> car = RaceCar("blue", 45, laps = 100)

>>> car.laps
0

Tag me if you need more help.

Marion Lackie
Marion Lackie
1,620 Points

Nice!
That makes more sense to me now. Thanks for the detailed response :)