From 021f081c38a754a74cc839874c562cc168efb18e Mon Sep 17 00:00:00 2001 From: vkchinta23 <161049962+vkchinta23@users.noreply.github.com> Date: Sun, 22 Sep 2024 16:09:41 +0530 Subject: [PATCH] Add files via upload --- car.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 car.py diff --git a/car.py b/car.py new file mode 100644 index 00000000000..26e3b2c5d5f --- /dev/null +++ b/car.py @@ -0,0 +1,42 @@ +class Car: + + def __init__(self, speed=0): + self.speed = speed + self.odometer = 0 + self.time = 0 + + def accelerate(self): + self.speed += 5 + + def brake(self): + self.speed -= 5 + + def step(self): + self.odometer += self.speed + self.time += 1 + + def average_speed(self): + return self.odometer / self.time + + +if __name__ == '__main__': + + my_car = Car() + print("I'm a car!") + while True: + action = input("What should I do? [A]ccelerate, [B]rake, " + "show [O]dometer, or show average [S]peed?").upper() + if action not in "ABOS" or len(action) != 1: + print("I don't know how to do that") + continue + if action == 'A': + my_car.accelerate() + print("Accelerating...") + elif action == 'B': + my_car.brake() + print("Braking...") + elif action == 'O': + print("The car has driven {} kilometers".format(my_car.odometer)) + elif action == 'S': + print("The car's average speed was {} kph".format(my_car.average_speed())) + my_car.step() \ No newline at end of file