I have a list that contains lists of 5 values and i'm getting an index out of range error when trying to run a while loop if a value in the list is == 5. Here is pretty much all my code
from solverFuncs import *
puzzle = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
pos = 0
backtrack = 0
checks = 0
while pos <25:
puzzle[pos/5][pos%5] += 1
checks+=1
if check_valid(puzzle,cages):
pos+=1
elif puzzle[pos/5][pos%5]==5:
while puzzle[pos/5][pos%5]==5:
puzzle[pos/5][pos%5]=0
pos=pos-1
backtrack+=1
The error im getting shows up as such:
File "solver.py", line 16, in <module>
while puzzle[pos/5][pos%5]==5:
IndexError: list index out of range
In python, list's indexes starts with [0]. So you have 5 elements in your list with min index = 0
and max index = 4
. Hope it will help
Whatever check_valid
does, I think you should add a check in the while block at line 16
while pos>=0 and puzzle[pos/5][pos%5]==5:
and also adding pos=0
before going back to while pos<25
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.