user2960198
user2960198

Reputation:

My code is producing indexError

My code provides the following error, would anybody be able to assist me in what is going wrong

if (RecentScores[count].Score) < (RecentScores[count + 1].Score): IndexError: list index out of range

def rankScores(RecentScores):
  noMoreSwaps = False
  while not noMoreSwaps:
    noMoreSwaps = True
    for count in range (1,len(RecentScores)):                                  
      if (RecentScores[count].Score) < (RecentScores[count + 1].Score):
        noMoreSwaps = False
        tempScore = RecentScores[count].Score
        tempName = RecentScores[count].Name
        RecentScores[count].Score = RecentScores[count+1].Score      
        RecentScores[count].Name = RecentScores[count+1].Name
        RecentScores[count+1].Score = tempScore
        RecentScores[count+1].Name = tempName
  DisplayRecentScores(RecentScores)

Would be extremely thankful if anyone could help

Upvotes: 0

Views: 39

Answers (3)

shuttle87
shuttle87

Reputation: 15934

for count in range (1,len(RecentScores)):                                  
  if (RecentScores[count].Score) < (RecentScores[count + 1].Score):

The biggest value count takes in this loop is the length of RecentScores. Then in the last iteration of the loop you try to access one past the end with: RecentScores[count + 1]

Trying to access this gives you the IndexError: list index out of range that you see.

Loop index starts at 0 so to fix this you need to change the range your loop operates on:

    for count in range (0,len(RecentScores)-1): 

If what you want to do is sort the entire list of scores as suggested by your comment (note that this isn't what the code in the question does but) then it would be better to do it like this:

sortedScores = sorted(RecentScores, key=lambda x: x.Score, reverse=True)

Upvotes: 0

wastl
wastl

Reputation: 2641

for count in range(0,len(RecentScores)-1): always remember that indices start at 0 and go to length-1. And since you are accessing something at index+1 you additionally have to substract 1, since range(a,b) goes from a to b-1

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34166

Index in most programming languages starts with 0. In the line

for count in range (1,len(RecentScores)): 

you are looping from 1 to length - 1 (I'm calling length to len(RecentScores)). But in the line

if (RecentScores[count].Score) < (RecentScores[count + 1].Score):

you are accessing to the list/tuple with the index

count + 1

Let's say the loop is in the last iteration. The value of count will be length - 1. Then, in the if condition, you try to access to the list/tuple with

RecentScores[length - 1 + 1]

which is equivalent to

RecentScores[length]

This will raise the exception because you are accessing to an index higher than the allowed.

How can you solve it?

In order to avoid using that not-allowed index, you can change your loop range to a smaller one:

for count in range (1, len(RecentScores) - 1): 

Upvotes: 1

Related Questions