-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.9p.py
65 lines (47 loc) · 1.94 KB
/
8.9p.py
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
def main():
print('Multi-leg Journey\'s fuel efficiency calculator.\n')
try:
# Get starting point of odometer
# No data validate since you need to check possible of Empty String enter
while True:
oStart = input('What\'s status of starting odometer: ')
if oStart == '': oStart = 0
# THIS GO ERROR IF ITS NOT STRING NUMBER!!
oStart = float(oStart)
if oStart < 0:
print('Error, correct your odometer.\n')
else:
break
# keep oStart to calculate Total MPG for all trip
oLast = oStart
gLast = 0
legCount = 1
legMPG = []
# Now collect all detail of each leg
# Sentinel Loop pattern
raw = input('\nLeg #{}, What\'s odometer status now? and how much gas been used?\n\
Please separate each data by space [Odometer Gas]:'.format(legCount))
while raw !='':
oTotal,gTotal = raw.split()
oTotal = float(oTotal)
gTotal = float(gTotal)
legMPG.append(round((oTotal-oLast)/(gTotal-gLast),1))
oLast = oTotal
gLast = gTotal
legCount = legCount+1
raw = input('\nLeg #{}, What\'s odometer status now? and how much gas been used?\n\
Please separate each data by space [Odometer Gas]:'.format(legCount))
if legCount > 1:
print('\nYour fuel efficiency are:')
# at this point legCount must be at least 1
for i in range(legCount-1):
print('Leg#{} has {} MPG.'.format(i+1,legMPG[i]))
#when trip end calculate total MpG of whole trip
print('\nYour total MPG is',round((oTotal-oStart)/gTotal,2))
else:
print('\nNo result.')
except ValueError:
print('\nPlease correct your input.')
except:
print('\nUnknow Error! Please try again.')
if __name__ =='__main__': main()