Monday, 15 June 2015

arrays - Error in python : name '...' is not defined -



arrays - Error in python : name '...' is not defined -

i using list store values.

sample code:

for in range(3): print lst[i] = i+1 print lst[i]

but getting error this:

lst[i] = i+1 nameerror: name 'lst' not defined

is required initialize array in python? wrong in code?

here's code:

in range(3): print lst[i] = i+1 print lst[i]

there 2 things wrong it

1- havn't initialize list, computer doesn't know yet working list (like in programs u maintain track of counter variable count = count + because must have seed value start with) 2- cannot append elements list assignment operator, utilize append function that.

so here's right code:

lst = [] in range(3): print(i) lst.append(i+1) print(lst[i])

output : 0 1 2 3

python arrays linux

No comments:

Post a Comment