-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschoolbus.py
More file actions
84 lines (65 loc) · 2.13 KB
/
Copy pathschoolbus.py
File metadata and controls
84 lines (65 loc) · 2.13 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
"""
There is a school bus that contains upto 1 driver and 10 students.
The school bus can drive ONLY IF there are 10 students and 1 driver
Solution:
class SchoolBus:
def __init__(self):
self.peoplelist = []
self.driverlist = []
def add_student(self):
stu_name = input('Enter student name:')
if len(self.peoplelist) < 10:
self.peoplelist.append(stu_name)
else:
print('You cannot add more student')
def add_driver(self):
dri_name = input('Enter driver name:')
if len(self.driverlist) < 1:
self.driverlist.append(dri_name)
else:
print("you need only one driver")
def drive(self):
if (len(self.peoplelist) == 10 and len(self.driverlist) == 1 ):
print('Bus full, we can go now')
else:
print('There are {} students and {} driver in the bus, you can\'t drive off'.format(len(self.peoplelist), len(self.driverlist)))
s = SchoolBus()
quit = True
while quit:
request = input(" 1. to enter student \n 2. to enter driver \n 3. to drive off \n 4. to Quit \n Answer: ")
if request == '1':
s.add_student()
elif request == '2':
s.add_driver()
elif request == '3':
s.drive()
elif request == '4':
quit = False
"""
#Using Polymorphism, this can be achieved by:
class Driver:
def __init__(self, name, liscense_check):
self.name = name
self.liscense_check = liscense_check
class Student:
def __init__(self, name):
self.name = name
class SchoolBus:
def __init__(self):
self.studentlist = []
self.driverlist = []
def add_student(self, stu):
self.studentlist.append(stu)
def add_driver(self, dri):
self.driverlist.append(dri)
def drive(self):
if len(self.studentlist) == 10 and len(self.driverlist)==1:
print('Drive on baby!')
else:
print('Number of passengers not allowed')
if __name__ == '__main__':
sb = SchoolBus()
for _ in range(10):
sb.add_student(Student('Tom'))
sb.add_driver(Driver('Nana', True))
sb.drive()