Untitled

mail@pastecode.io avatar
unknown
python
2 months ago
1.2 kB
0
Indexable
Never
class Car:
    def __init__(self,name,max_speed):
        self.name = name
        self.speed = 0
        self.state = 'off'
        self.max_speed = max_speed

    def start(self):
        if self.state == "off":
            print("Turning car on..")
            self.state = "on"
        else:
            print("Car is already on.")

    def stop(self):
        if self.state == "on":
            print("Turning car off..")
            self.state = "off"
        else:
            print("Car is already off.")

    def accelerate(self,speed):
        if self.state == "on":
            new_speed = self.speed + speed
            if new_speed >= self.max_speed:
                self.speed = self.max_speed
            else:
                self.speed = new_speed
            print(f"Current speed:{self.speed}")

    def brake(self,speed):
        if self.state == "on":
            new_speed = self.speed - speed
            if new_speed <= 0:
                self.speed = 0
            else:
                self.speed = new_speed
            print(f"Current speed:{self.speed}")

omni = Car(name="Maruthi Omni",max_speed=100)


Leave a Comment