What's wrong with this code snippet?

Brian van den Broek broek at cc.umanitoba.ca
Wed Jan 4 17:41:17 EST 2006


Karlo Lozovina said unto the world upon 04/01/06 04:19 PM:
> Here is it:
> 
> ---
> class Human:
>     def __init__(self, eye_one, eye_two):
>         self.eye_one = eye_one
>         self.eye_two = eye_two
>         
> class Population:
>     def __init__(self):        
>         self.house = []
>         for i in range(0, POPULATION_COUNT):
>             self.house.append(Human(self.GenerateRandomColour(), 
>                                     self.GenerateRandomColour()))
>             
>     def GenerateRandomColour():
>         rn.seed()
>         colour = rn.choice(['C', 'P', 'Z'])
>         return colour
> ---
> 
> Uppon running it gives this error:
> 
> ---
> Initializing first generation population:
> Traceback (most recent call last):
>   File "population.py", line 38, in ?
>     earth = Population()
>   File "population.py", line 26, in __init__
>     self.house.append(Human(self.GenerateRandomColour(), 
> self.GenerateRandomColour()))
> TypeError: GenerateRandomColour() takes no arguments (1 given)
> ---
> 
> If I remove GenerateRandomColour from class definition, and put it as a 
> separate function, everything works fine. I've been staring at this code 
> for half an hour and can't find what's wrong :(.


You left out self in your GenerateRandomColour method definition.

It should be:

      def GenerateRandomColour(self):
          rn.seed()
          colour = rn.choice(['C', 'P', 'Z'])
          return colour

HTH,

Brian vdB



More information about the Python-list mailing list