问题描述:

I wrote the following two functions:

def place_walls(level):

wall_cords = level[2].split("\n")

for cord in wall_cords:

process_coordinate(cord)

def process_coordinate(coordinate):

cords = coordinate.split()

print cords[0]

Python returns a list index out of range error. If I print the cords variable without an index it prints all the cords in a list, however the last list is an empty list. Might that be the problem, how could that be solved?

The input level looks like this:

1 0

0 0=r=3 3

4 3

5 3

6 3

7 3

8 3

9 3

10 3

11 3

3 4

网友答案:

Assuming it is just the last, empty list you have a problem with: clearly, there is no 1st element to print in that case, so wrap it in an if statement (for example if len(cords)>0:; if the list is empty, do whatever is more appropriate for that circumstance.

相关阅读:
Top