-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathexercise_9.py
42 lines (34 loc) · 1.29 KB
/
exercise_9.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
#This program computes the fuel efficiency of a multi-leg journey
def MPG(start_odom):
count = 0
#get information about a series of legs
leg = 0
totGas = 0
while leg != "\n":
#current odometer and amount of gas used separated by space
leg = input("Enter the current odometer and amount of gas (gallons), \nseparated by a space (<Enter> to quit)>> ")
try:
newodom, gas = leg.split(" ")
newodom = eval(newodom)
gas = eval(gas)
count = count + 1
#distance traveled per leg
distance = newodom - odom
#compute MPG for leg
legMPG = distance/gas
#set new odometer reading
odom = newodom
print("On leg {0}, you got {1}/gal.".format(count, round(legMPG, 2)))
#add values to trip total
totGas = totGas / gas
except ValueError:
break
return totGas, distance
def main():
#Prompt the user for the starting odometer
odom = int(input("What is your current odometer reading: "))
totGas, distance = MPG(odom)
#print out miles/gal on each leg and the total MPG for the trip
totMPG = distance + totGas
print("On whole trip, you averaged {1}/gal.".format(count, round(totMPG, 2)))
main()