Why does this code throw an IndexError only when run on my CA?

Riyan Shah

I have a .csv file with names and scores. Here are the files contents (The name and score are in separate columns):

Bob 1
Dave 6
Linda 9.76
Andy 90
hilary 87
mathew 6.4576589 

The program should display the scores from highest to lowest. Like this:

Bob 1
Dave 6
mathew 6.4576589 
Linda 9.76
hilary 89
Andy 90

I have been trying this for ages. Here is my code below:

import csv
import operator
out_file = open('class1_scores_max.csv','r')
scores1 = csv.reader(out_file,delimiter=',')
sort = sorted(scores1,key = lambda x: float(x[1]))
for eachline in sort:
    final = eachline[0]," ",eachline[1]
    print (''.join(final))

This work perfectly in a separate python file but in my main Controlled Assessment in school it comes up with the error:

IndexError: list index out of range

In my main code at school it is part of a definition (sub-routine). Can anyone help?

user3835277

From what you've provided, you're getting an IndexError at either of these two lines:

sort = sorted(scores1,key = lambda x: float(x[1]))

or

    final = eachline[0]," ",eachline[1]

I suspect that you're IndexError is at the first line, because getting an IndexError at the second line seems to imply that it would've occured at the first as well.

You can fix this by making sure there's two elements in each line before sorting and printing them.

sort = sorted([x for x in scores1 if len(x) == 2], key = lambda x: float(x[1]))

This is also a great time to learn the basics of debugging. Suppose you didn't have help from the internet and had to debug this problem on your own. Adding a few print statements is one of the quickest and simplest ways to debug code. For example,

import csv
import operator
out_file = open('class1_scores_max.csv','r')
scores1 = csv.reader(out_file,delimiter=',')
for score in scores1:
    print score
sort = sorted(scores1,key = lambda x: float(x[1]))
for eachline in sort:
    final = eachline[0]," ",eachline[1]
    print (''.join(final))

Notice where I inserted the following lines:

for score in scores1:
    print score

This will allow you to see if there's anything in scores1 that might lead to an IndexError later on when you try to access the first and second elements of a an element in scores1.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

From Dev

Why does my javascript only fire when the code is repeated?

From Dev

Why does javac throw compile errors when my IDE does not?

From Dev

Why does javac throw compile errors when my IDE does not?

From Dev

Why does my for-each only throw one exception?

From Dev

why does my loop only run once?

From Dev

Why does Android Studio throw away my Code Style settings?

From Dev

Why does my random variable code throw a TypeError?

From Dev

Why does datetime.strptime throw an error when run with Django?

From Dev

Why does my code run into an infinite loop?

From Dev

Why does my code print [I@87816d when i run this code?

From Dev

Why does this code not throw a NullPointerException?

From Dev

Why does this code not throw an exception?

From Dev

why does this code throw a IllegalMonitorStateException?

From Dev

Why does this code not throw a NullPointerException?

From Dev

Why does this code throw a MalformedURLException?

From Dev

Why does my Image stretch when I run my app?

From Dev

Why does this code raise IndexError in certain situation?

From Dev

why setTimeout() only run my code once,at first time?

From Dev

Why does my for loop only appear to run once?

From Dev

Why does my script that uses twilio run perfectly locally, but throw an error on Python Anywhere?

From Dev

Why does dom4j throw InvalidXPathException for a valid XPath only in my test environment?

From Dev

Why does typeof only sometimes throw ReferenceError?

From Dev

Why does my code run without 'Option Explicit' but fail with it?

From Dev

Why does my julia code run so slowly?

From Dev

Why does my Haskell code not appear to run in Parallel

From Dev

Why does Python run my code bottom to top?

From Dev

Why does my rake file code run every time?

From Dev

Why does sbt run throw Overlapping output directories with Slick's code generator?

Related Related

  1. 1

    currency converter- why does my for loop in switch wont work? when i run my code only the first for loop works

  2. 2

    Why does my javascript only fire when the code is repeated?

  3. 3

    Why does javac throw compile errors when my IDE does not?

  4. 4

    Why does javac throw compile errors when my IDE does not?

  5. 5

    Why does my for-each only throw one exception?

  6. 6

    why does my loop only run once?

  7. 7

    Why does Android Studio throw away my Code Style settings?

  8. 8

    Why does my random variable code throw a TypeError?

  9. 9

    Why does datetime.strptime throw an error when run with Django?

  10. 10

    Why does my code run into an infinite loop?

  11. 11

    Why does my code print [I@87816d when i run this code?

  12. 12

    Why does this code not throw a NullPointerException?

  13. 13

    Why does this code not throw an exception?

  14. 14

    why does this code throw a IllegalMonitorStateException?

  15. 15

    Why does this code not throw a NullPointerException?

  16. 16

    Why does this code throw a MalformedURLException?

  17. 17

    Why does my Image stretch when I run my app?

  18. 18

    Why does this code raise IndexError in certain situation?

  19. 19

    why setTimeout() only run my code once,at first time?

  20. 20

    Why does my for loop only appear to run once?

  21. 21

    Why does my script that uses twilio run perfectly locally, but throw an error on Python Anywhere?

  22. 22

    Why does dom4j throw InvalidXPathException for a valid XPath only in my test environment?

  23. 23

    Why does typeof only sometimes throw ReferenceError?

  24. 24

    Why does my code run without 'Option Explicit' but fail with it?

  25. 25

    Why does my julia code run so slowly?

  26. 26

    Why does my Haskell code not appear to run in Parallel

  27. 27

    Why does Python run my code bottom to top?

  28. 28

    Why does my rake file code run every time?

  29. 29

    Why does sbt run throw Overlapping output directories with Slick's code generator?

HotTag

Archive