@@ -0,0 +1,1742 @@
+#Used pprint for debugging%0Afrom pprint import pprint%0A%0Adef readFile():%0A    #This function returns the file as a list where every line is an element%0A    try:%0A        myFile = open(%22cars.dat.txt%22,%22r%22)%0A        #myFile = open(input(%22Please write the name of the file including extension: %22),%22r%22)%0A    except:%0A        print (%22There is no such file%22)%0A        readFile()%0A    fileList = %5B%5D%0A    for line in myFile:%0A        fileList.append(line)%0A    return fileList%0A%0Adef dataExtractor(list_,start,end):%0A    #This function uses substrings to create a list with only the data needed%0A    dataList = %5B%5D%0A    for index in range(0,len(list_)):%0A        if index %25 2 == 0:%0A            evenLine = list_%5Bindex%5D%0A            dataList.append(evenLine%5Bstart:end%5D)%0A    return dataList%0A%0Adef avgInList(list_):%0A    #This function takes a list and returns the average of its elements%0A    avgSum = 0%0A    for element in list_:%0A        element = float(element)%0A        avgSum += element%0A    average = avgSum/len(list_)%0A    return average%0A#We create the list%0AcsvFile = readFile()%0A'''%0A    Here we create the 3 lists that containg the data for%0A    Miles per Gallon in City and Highway and the average midrange%0A    Price of the vehicles%0A'''%0AcityMPG = dataExtractor(csvFile,52,54)%0A%0AhighwayMPG = dataExtractor(csvFile,55,57)%0A%0AavgPrice = dataExtractor(csvFile,42,46)%0A%0A#We take the values, get the average, then convert them to float%0A%0AavgCityMPG = float(%22%25.2f%22 %25 avgInList(cityMPG))%0A%0AavgHighwayMPG = float(%22%25.2f%22 %25 avgInList(highwayMPG))%0A%0AavgPrice = float(%22%25.2f%22 %25 avgInList(avgPrice))%0A%0A#Simply print the results%0Aprint (%22The average MPG in the city is: %7B%7D%5Cn %5C%0AThe average MPG in the Highway is: %7B%7D%5Cn %5C%0AThe average price of these cars is: %7B%7D%22.format(avgCityMPG,avgHighwayMPG,avgPrice))%0A

Add files via upload