Gallons of gasoline used in 100 miles of driving. TABLE 5.11 Model Gal Model Gal Model Gal Accord 2.1 Accord 4.3 Prius 4.1 2.3 Camry Sebring Camry 3.8 Prius 4.1 4.2 4.2 Camry Mustang 3.9 Camry 5.2 Accord Mustang 5.3 4.4 Model MPG Prius 45.45 25.00 Camry Sebring 23.81 23.44 Accord Mustang 19.05 Outcome of Programming Project 6. FIGURE 5.51 1 inp=open('Mileage.txt','r') #reading the file 2 inp=inp.readlines() #reading every line from file 3 inp=[i.rstrip('\n') for i in inp] #removing the next line character from every Line 4 inp=[i.split() for i in inp] #spliting every Line into make and gallons 5 inp=[[i[0],float(i[1])] fori in inp] #converting the gallon to float 6 cars={} #empty dictionary 7 for i in inp: if cars.get(i[0],e)==0: 8. cars[i[0]]=(1,i[1]) #creating new tuple else: 10 count, mileage=cars[i[0]] 11 12 count+=1 mileage+-i[1] cars[i[0]]=(count, mileage) #updating tuple 13 14 15 #empty lists to store make and mileage 16 makes=[] 17 mileages-[] 18 for i in cars: makes.append (i) 19 j=cars[i] mileages.append(round(j[0]*100/j[1],2)) 20 21 22 #sorting mileage and make according to mileage in ascending order 23 zipped_pairs=zip(mileages,makes) 24 makes=[x for , x in sorted(zipped_pairs)] 25 mileages.sortT) 26 #reversing the list to make it descending order 27 makes-makes[::-1] 28 mileages=mileages[::-1] 29 #output 30 print(' 31 for i,j in zip(makes, mileages): Model MPG') print('%85 %7.2f' %(i,j)) 32
In Python 3.7:
A fuel-economy study was carried out for five models of cars. Each car was driven 100 miles, and then the model of the car and the number of gallons used were placed in a line of the file Mileage.txt. (Formula is 100 divided by gallons used, rounded up to the nearest .hundredth, as in "11.11")
Table 5.11 shows the data for gallons used. Write a
---------------------
I already did it. I'm just asking if you can check my work. I included pictures of the table and the code, as well has providing the code below. Thank you.
-----------------------
''' This program create a dictionary of five items, with a key for each model,
and a two-tuple for each value. '''
inp=open('Mileage.txt','r') #reading the file
inp=inp.readlines() #reading every line from file
inp=[i.rstrip('\n') for i in inp] #removing the next line character from every line
inp=[i.split() for i in inp] #spliting every line into make and gallons
inp=[[i[0],float(i[1])] for i in inp] #converting the gallon to float
cars={} #empty dictionary
for i in inp:
if cars.get(i[0],0)==0:
cars[i[0]]=(1,i[1]) #creating new tuple
else:
count,mileage=cars[i[0]]
count+=1
mileage+=i[1]
cars[i[0]]=(count,mileage) #updating tuple
#empty lists to store make and mileage
makes=[]
mileages=[]
for i in cars:
makes.append(i)
j=cars[i]
mileages.append(round(j[0]*100/j[1],2))
#sorting mileage and make according to mileage in ascending order
zipped_pairs=zip(mileages,makes)
makes=[x for _, x in sorted(zipped_pairs)]
mileages.sort()
#reversing the list to make it descending order
makes=makes[::-1]
mileages=mileages[::-1]
#output
print(' Model MPG')
for i,j in zip(makes,mileages):
print('%8s %7.2f' %(i,j))
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images