2

I am making a simple Checkers again. And, in order to keep track of all the locations for the checkers to move to, I created a list to add all of the locations.

Locations = [(10, 10), (70, 10), (130, 10), (190, 20), (250, 10), (310, 10), \
         (370, 10), (430, 10), (10, 70), (130, 70), (190, 70), (250, 70), \
         (310, 70), (370, 70), (430, 70), (10, 130), (70, 130), (130, 130), \
         (190, 130), (250, 130), (310, 130), (370, 130), (430, 130), \
         (10, 190), (70, 190), (130, 190), (190, 190), (250, 190), (310, 190), \
         (370, 190), (430, 190)]

However, when I try to execute the program, I always get: Ld8 = Locations[31] - list index out of range

So, I thought maybe a list can only contain certain amount of numbers. So, I created a second Locations list and added rows E - H, so to split up the list. But, I still receive the same error of index being out of range. (Ld8 is the variable storing the location for Row D, Column 8)

2
  • Check the length of your Locations variable to see if it is greater than 31: print len(Locations) Commented Oct 13, 2011 at 14:58
  • You might find it easier to work with a fixed-length list of 64 items representing the squares of the checkerboard, with values indicating what's present on the square. Commented Oct 13, 2011 at 16:21

6 Answers 6

6

You have a very basic understanding problem:

>>> Locations = [(10, 10), (70, 10), (130, 10), (190, 20), (250, 10), (310, 10), (370, 10), (430, 10), (10, 70), (130, 70), (190, 70), (250, 70), (310, 70), (370, 70), (430, 70), (10, 130), (70, 130), (130, 130), (190, 130), (250, 130), (310, 130), (370, 130), (430, 130), (10, 190), (70, 190), (130, 190), (190, 190), (250, 190), (310, 190), (370, 190), (430, 190)]
>>> len(Locations)
31
>>> Locations[0] # first element
(10, 10)
>>> Locations[30] # last element
(430, 190)
>>> Locations[31] # doesn't exist!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

The elements in Locations go from 0 to 30, because there are 31 elements in total.

Sign up to request clarification or add additional context in comments.

Comments

3

List indexes start from 0. So your list has 31 items you can access them with indexes 0-30. Locations[31] refers to the 32nd item which doesn't exist

Comments

2

The largest index in that list is 30, not 31. Indexes in lists (in most languages) start with zero. So (10,10) is at index 0 and (430,190) is at index 30.

If you are trying to index at the end of the list in python, I suggest you index by Locations[-1].

If you are trying to use len, you need to subtract 1: Locations[len(Locations) - 1]

Comments

1

Emm... The list's elements are numbered from 0, not from 1. I have counted your list and I see elements from 0 to 30.

Sometimes I even forget it's possible to start counting at 1...

Comments

0

Locations[31] tries to access the 32nd item in the list (which doesn't exist)

Comments

0

Indexes of arrays for most programming languages start at 0 and not 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.