Count from 0 to X possible, count from 1 to x gives error 'IndexError: list assignment index out of range' python -
i want count 1 65 , wright string keep receiving following error
indexerror: list assignment index out of range
i use te following code
file = [] in range(1,65): print (i) file.append(i) file[i] = 'file' + str(i)
i know there simpler ways count necessary file name can wright csvfile.
strange thing when use same code change (1,65) (0,64) works fine. counting 0 tot 64 gives me no error crucial program count 1 65.
i have no idea way problem frustrates me code simpel , 1 simple change of numbers makes code behave different.
every suggestions or appreciated!
kind regards
use following adjustment code:
file = [] in range(1,65): print (i) file.append(i) file[i-1] = 'file' + str(i)
file[i]
doesn't exist yet because python indexes zero. so, when append i
file
, location of i
@ index i-1
as aside, use list comprehension make neater:
file = ['file{}'.format(i) in range(1, 65)]
or:
file = ['file' + str(i) in range(1, 65)]
both produce same result , don't require indexing.
Comments
Post a Comment