Created
March 23, 2020 22:34
-
-
Save giljr/fa7b96797c97fd95e634772436cca329 to your computer and use it in GitHub Desktop.
Correcting the class-
typo: accelarate = accelerate
Studing Python 3 - 03/2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car: | |
def __init__(self, speed=0): | |
self.speed = speed | |
self.odometer = 0 | |
self.time = 0 | |
def say_state(self): | |
print("I'm going {} kph!".format(self.speed)) | |
def accelerate(self): | |
self.speed += 5 | |
def brake(self): | |
if self.speed < 5: | |
self.speed = 0 | |
else: | |
self.speed -= 5 | |
def step(self): | |
self.odometer += self.speed | |
self.time += 1 | |
def average_speed(self): | |
if self.time != 0: | |
return self.odometer / self.time | |
else: | |
pass | |
if __name__ == '__main__': | |
my_car = Car() | |
print("I'm a car!") | |
while True: | |
action = input("What should I do? [A]ccelarate, [B]rake, " | |
"show [O]dometer, or show averange [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.accelarate() | |
elif action == 'B': | |
my_car.brake() | |
elif action == 'O': | |
print("The car has driven {} kilometers".format(my_car.odometer)) | |
elif action == 'S': | |
print("The car averange speed was {} kph".format(my_car.average_speed())) | |
my_car.step() | |
my_car.say_state() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment