In: Computer Science
id | manufacturer | model | displ | year | cyl | trans | drv | cty | hwy | fl | class |
1 | audi | a4 | 1.8 | 1999 | 4 | auto(l5) | f | 18 | 29 | p | compact |
2 | audi | a4 | 1.8 | 1999 | 4 | manual(m5) | f | 21 | 29 | p | compact |
3 | audi | a4 | 2 | 2008 | 4 | manual(m6) | f | 20 | 31 | p | compact |
4 | audi | a4 | 2 | 2008 | 4 | auto(av) | f | 21 | 30 | p | compact |
5 | audi | a4 | 2.8 | 1999 | 6 | auto(l5) | f | 16 | 26 | p | compact |
6 | audi | a4 | 2.8 | 1999 | 6 | manual(m5) | f | 18 | 26 | p | compact |
7 | audi | a4 | 3.1 | 2008 | 6 | auto(av) | f | 18 | 27 | p | compact |
8 | audi | a4 quattro | 1.8 | 1999 | 4 | manual(m5) | 4 | 18 | 26 | p | compact |
9 | audi | a4 quattro | 1.8 | 1999 | 4 | auto(l5) | 4 | 16 | 25 | p | compact |
10 | audi | a4 quattro | 2 | 2008 | 4 | manual(m6) | 4 | 20 | 28 | p | compact |
11 | audi | a4 quattro | 2 | 2008 | 4 | auto(s6) | 4 | 19 | 27 | p | compact |
12 | audi | a4 quattro | 2.8 | 1999 | 6 | auto(l5) | 4 | 15 | 25 | p | compact |
Output file should contain the calculation output for: o Python
code properly runs and calculates the average city mileage for all
vehicles (avg_city) – 15 pts o Python code properly runs and
calculates the average highway mileage for all vehicles (avg_hwy) –
15 pts o Python code properly runs and calculates the average
highway mileage of all Ford vehicles (ford_hwy) – 20 pts o Python
code properly runs and calculates the average city mileage of all
SUV vehicles (suv_city) – 20 pts • Only iterate through mpg.csv
once, minus 10 points for each loop through mpg.csv more than
once
*****
student_name='Enter your name here'
paws_id='A123'#enter our paws id
sum_cty=0
n=0
sum_hwy=0
sum_ford_hwy=0
n_ford_hwy=0
sum_suv_city=0
n_suv_city=0
firstLine=True
with open('MPG.csv','r') as f:
for line in f:
if firstLine:
firstLine=False
else:
data=line.split(',')
sum_cty+=float(data[8])
sum_hwy+=float(data[9])
n=n+1
if data[1].lower()=='ford':
sum_ford_hwy+=float(data[9])
n_ford_hwy+=1
if data[-1].strip().lower()=='suv':
sum_suv_city+=float(data[8])
n_suv_city+=1
avg_city=sum_cty/n
avg_hwy=sum_hwy/n
ford_hwy=sum_ford_hwy/n_ford_hwy
suv_city=sum_suv_city/n_suv_city
f=open(paws_id+'_assignment4.txt','w')
f.write('Average city mileage for all vehicles:
{:.4f}\n'.format(avg_city))
f.write('Average highway mileage for all vehicles:
{:.4f}\n'.format(avg_hwy))
f.write('Average highway mileage of all Ford vehicles:
{:.4f}\n'.format(ford_hwy))
f.write('Average city mileage of all SUV vehicles:
{:.4f}\n'.format(suv_city))
f.close()