Ask coding questions

← Back to all posts
List Range Error
Codeverse (158)

Hello there! I have a list range error in my code. I can't seem to find the error. Could I get some help on this? It is written in Python by the way.

The error is happening in the backtracking section of my code. It is for if the scrambling algorithm does a move and then the opposite of that move, which would be backtracking.

Thank you very much for sparing your time to solve my question!

Answered by SixBeeps (5331) [earned 5 cycles]
View Answer
SixBeeps (5331)

Careful, you're using the same variable names for your outer loops and your inner loops. Change one of them to j or something.

Also, I'd suggest a different method. Instead, only store the faces of each cube (just U, D, L, etc. without the primes or the 2's). WHen you are choosing a move, rechoose if it affects the same face as it did before. Something like this?

by2 = ['U', 'D', 'R', 'L', 'B', 'F']
additives = ['', '\'', '2']
lastMove = ''

def nextMove(moveset):
  global lastMove
  thisMove = lastMove
  while (thisMove == lastMove):
    thisMove = choice(moveset)
  lastMove = thisMove
  return thisMove + choice(additives)

scramble = ''
for move in range(20):
  scramble += nextMove(by2) + ' '