-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevator.py
More file actions
84 lines (75 loc) · 3.42 KB
/
Elevator.py
File metadata and controls
84 lines (75 loc) · 3.42 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Elevator:
'''Represents a simple elevator'''
def __init__(self, startFloor, startDoorsOpen):
'''Elevator(startFloor, startDoorsOpen) -> Elevator
Constructs an Elevator
startFloor: int giving the starting floor
startDoorsOpen: bool giving the starting doors
(True = 'open')'''
self.floor = startFloor # store floor attribute
self.doorsOpen = startDoorsOpen # store doors attribute
self.passengers = [] # list of passengers - starts empty
def __str__(self):
'''str(Elevator) -> str
Returns a string giving the floor and state of the doors.'''
answer = 'doors ' # will contain string to return
if self.doorsOpen: # if doors open
answer += 'open' # say so
else: # if doors closed
answer += 'closed' # say that too
answer += ', floor ' # this is in every answer
answer += str(self.floor) # add floor number
answer += ', passengers: ' # passenger list
for person in self.passengers:
answer += person + ' '
return answer
def open_doors(self):
'''Elevator.open_doors()
Opens the doors by setting doors attribute to True.'''
self.doorsOpen = True # set doors to open
def close_doors(self):
'''Elevator.close_doors()
Closes the doors by setting doors attribute to False.'''
self.doorsOpen = False # set doors to closed
def go_up(self):
'''Elevator.go_up()
Goes up by one floor if doors are not open.'''
if self.doorsOpen: # if doors are open
print('Please close doors!') # print warning
else: # if doors are closed
self.floor += 1 # increase floor by 1
def go_down(self):
'''Elevator.go_down()
Goes down by one floor if doors are not open.'''
if self.doorsOpen: # if doors are open
print('Please close doors!') # print warning
else: # if doors are closed
self.floor -= 1 # decrease floor by 1
def go_to_floor(self, destination):
'''Elevator.go_to_floor(int)
Closes doors, moves to destination, and opens doors.'''
if self.doorsOpen: # if doors are open
self.close_doors() # close 'em
while self.floor != destination: # if not at destination
if self.floor < destination: # if below
self.go_up() # go up 1 floor
else: # if above
self.go_down() # go down 1 floor
self.open_doors() # open doors
def get_on(self, person):
'''Elevator.get_on(person)
Lets person on elevator (if doors are open)'''
if self.doorsOpen:
self.passengers.append(person)
else:
print("Can't get on while doors are closed!")
def get_off(self, person):
'''Elevator.get_off(person)
Lets person off elevator (if doors are open)'''
# check if person is on first
if person not in self.passengers:
print(person + " is not on the elevator!")
elif self.doorsOpen:
self.passengers.remove(person)
else:
print("Can't get off while doors are closed!")