Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #819

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions car.py
Original file line number Diff line number Diff line change
@@ -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()