In: Computer Science
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:
for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):
index +=1
print("{}. {} {:.2f}".format(county, sums_by_volume[county]))
When I run this, it gives me an IndexError: Replacement index 2 out of range for positional args tuple.
Below is my entire code. I don't know what I am doing wrong. Could you please help?
from csv import reader
#Asks for name of input files
#Opens and reads the fiel with a CSV reader
#Sums the sales by volumn for each rows county
#Sum total Sales in Iowa
#Prints the total country sales ordered by volume
UserInput = input("Please enter a file name: ")
sums_by_volume = {}
index = 0
if (UserInput == 'Iowa-Liquor-Sales-2019.csv'):
inputFile = UserInput
print('Success! File Found!')
else:
print ('No Such File Found! Please Restart The Program And Enter A New File')
quit()
with open(inputFile, 'r') as csvFile:
csvreader = reader(csvFile)
headers = next(csvreader)
for row in csvreader:
COUNTY_NAME = row[9].upper()
VOLUME_IN_LITERS = float(row[22])
if COUNTY_NAME not in sums_by_volume:
# print('This is a test message')
sums_by_volume[COUNTY_NAME] = 0.0
sums_by_volume[COUNTY_NAME] += VOLUME_IN_LITERS
#SortedSum = sorted(sums_by_volume.values(), reverse = True)
#print(SortedSum)
for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):
index +=1
print("{}. {} {:.2f}".format(county, sums_by_volume[county]))
#print(SortedSum, + " ", COUNTY_NAME)
print('End of Program')
Hi friend,
You did everything great, but there is a very minute mistake while you are printing the values, let me help you regarding this,
Index error:
Index error usally occurs when we are trying to fetch the data that is not present or which is out of bounds.
so your program encounterd an Index error, the first thing we need to do is, we should find where we are trying to fetch data that is out of bounds.
After travesing the code you have written, I found this line which is causing this Index out of bounds error
line in your code that is giving error
print("{}. {} {:.2f}".format(county, sums_by_volume[county]))
position of the line in code: from bottom 3rd line
Note: the red colored {} is causing you index out of bounds error
The error you commited is
you are giving 3 placeholders ( {}. {} {:2f} ) but you are filling them with 2 values (.format(country, sum_by_volume[country]) , so third placeholder is waiting for value which you did not specify that is the reason you are getting index out of bounds error.
Correct line of code that should be replaced with the above line
print("{}. {:.2f}".format(county, sums_by_volume[county]))
apart from this minute mistake you did a great job, please check now after changing the code if you still have any problem please mention your query in comment box, I will reach to you as soon as possible,
If your code works fine please given a THUMBS UP,
Thank you in advance. :)