简体   繁体   中英

Index out of range , Python?

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).

[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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM