[Tutor] TypeError I don't understand
Vincent Wan
wan at walrus.us
Wed Jan 12 19:58:45 CET 2005
Dear All,
python gives me a type error that I don't understand
running my program gives me "TypeError: len() of unsized object"
but I know I that the object whose length I asked for is a list and
therefor a sized type
here is a sample of the output:
type (living_species) : <type 'list'>
len(living_species) : 1
Traceback (most recent call last):
File "new_prog1b.txt", line 15, in ?
while counter < len(living_species):
TypeError: len() of unsized object
here is the code:
import random
living_species = [0]
dead_species = []
tree = "*0*"
current_time = 0
end_of_time = 25
current_species = 0
branching_probablity = .1
extinction_probablity = .1
while current_time < end_of_time:
counter = 0
while counter < len(living_species):
if random.random() < branching_probablity:
current_species += 1
living_species.append(current_species)
target = '*' + str(living_species[counter]) + '*'
replacement = '(' + target + ',*' + str(current_species) +
'*)'
tree = tree.replace(target, replacement)
counter += 1
# PROBLEM HERE
print 'type (living_species) :',type(living_species) # proves
living_species is a list
print 'len(living_species) :', len(living_species) # proves
length living_species is a number
counter = len(living_species) - 1 # python says TypeError: len()
of unsized object
while counter >- 1:
if random.random() < extinction_probablity:
dead_species = dead_species.append(counter)
living_species = living_species.remove(counter)
counter -= 1
current_time += 1
print living_species
print dead_species
tree = tree.replace('*','')
print tree
Thank You,
Vincent
------------------------------------------------------------------------
--------------
PhD Candidate
Committee on the Conceptual and Historical Studies of Science
University of Chicago
PO Box 73727
Fairbanks, AK 99707
wanATwalrusDOTus (replace AT with "@" and dot with ".")
More information about the Tutor
mailing list