[Tutor] TypeError I don't understand

Jacob S. keridee at jayco.net
Thu Jan 13 02:17:52 CET 2005


Danny Yoo solved the problem, but I would like to suggest something.

target = '*' + str(living_species[counter]) + '*'
replacement = '(' + target + ',*' + str(current_species) +  '*)'

change these lines to

target = "*%s*" % (living_species[counter])
replacement = "(%s*%s*)" % (target,current_species)

It will (obviously) work your way, but I think that string formatting
simplifies and improves readability, IMHO.

HTH,
Jacob Schmidt


> 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 ".")
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>



More information about the Tutor mailing list