Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Software (http://www.velocityreviews.com/forums/f6-software.html)
-   -   Python list problems (http://www.velocityreviews.com/forums/t742408-python-list-problems.html)

wanna-python 01-22-2011 01:26 AM

Python list problems
 
Newby alert! - I'm new to Python

Why does this program do the following:

list-test.py

def reset_list(list):
list = []
for i in range(0,9):
list.append('')
return list

def put_in_list(i):
list[i] = i
return

reset_list(list)

for i in range(1,5):
put_in_list(i)

c:\python27\dev>python list-test.py
Traceback (most recent call last):
File "list-test.py", line 14, in <module>
put_in_list(i)
File "list-test.py", line 8, in put_in_list
list[i] = i
TypeError: 'type' object does not support item assignment

Vort3x 02-28-2011 06:30 PM

This is pretty messy, please indent the code so we can see whats going on.

Vort3x 02-28-2011 06:48 PM

Your list you use in the first method is only local to that method I think?
This should work.

def reset_list(list):
for i in range(0,9):
list.append('')
return list

def put_in_list(i):
list[i] = i
return

list = []
reset_list(list)

for i in range(1,5):
put_in_list(i)

Vort3x 02-28-2011 06:49 PM

Hmm, mine isnt indented either sorry, how do you indent?


All times are GMT. The time now is 07:23 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57