1

My program is supposed to auto-fill (like ms paint) a text file. I cannot figure out why this is giving me an index out of range.

Also my fill function must be recursive, and making the call to neighboring cells in the following order: above,right,below, and left. Better or more efficient ways to do this please let me know(that's if i'm even doing what I want it to be doing).

4
  • 1
    Please show us full stacktrace! Commented Dec 3, 2015 at 5:44
  • 1
    This is surely unrelated to your exception, but input(print("...")) doesn't do what you want. It prints the prompt, but then also prints None as well. You don't need to use print in that context, input prints its argument already. Commented Dec 3, 2015 at 5:49
  • @ozgur is that what your looking for? Commented Dec 3, 2015 at 5:54
  • @Blckknght honestly I was wondering where the None would randomly come from, thank you. Commented Dec 3, 2015 at 5:57

1 Answer 1

2

[int(row)][int(col)] doesn't work as you expect.

It creates a list with one element, which is int(row) and then tries to access its int(col) element. Actually this line will raise an exception every time int(col) is > 0.

Instead, you should use a tuple:

p = int(row), int(col)

but keep in mind that it is immutable, so you can't change it directly later:

p = int(row), int(col)
p[0] = 3
>> TypeError: 'tuple' object does not support item assignment

Though you can reassign a new tuple:

p = int(row), int(col)
p = 3, int(col)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this. Ive change it to a tuple , now its giving me a list index out of range at , if board[pos[0]][pos[1]+1] == ' ':
It is the same error for the same reason, so apply the same solution :)

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.