-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcar.py
More file actions
executable file
·47 lines (35 loc) · 1.06 KB
/
car.py
File metadata and controls
executable file
·47 lines (35 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
# Naz-Al Islam
# 04/18/16
# Car racing game
class Car:
def __init__(self, start_pos, speed):
self.carPos = start_pos
self.carSpeed = speed
def drive(self, time, direction):
self.drvTime = time
self.drvDirection = direction
if self.drvDirection == 'forward':
self.carPos = self.carPos + self.drvTime * self.carSpeed
elif self.drvDirection == 'backward':
self.carPos = self.carPos - self.drvTime * self.carSpeed
def printPosition(self):
print('The car is currently at position ' + str(self.carPos))
def main():
myCar = Car(2, 3)
myCar.printPosition()
myCar.drive(3, 'forward')
myCar.printPosition()
myCar.drive(2, 'backward')
myCar.printPosition()
print()
newCar = Car(5, 7)
newCar.printPosition()
newCar.drive(4, 'forward')
newCar.printPosition()
newCar.drive(3, 'backward')
newCar.printPosition()
newCar.drive(1, 'backward')
newCar.printPosition()
if __name__ == '__main__':
main()