I have no class

Seymore4Head Seymore4Head at Hotmail.invalid
Sun Nov 23 04:35:43 CET 2014


On Sat, 22 Nov 2014 22:14:21 -0500, Ned Batchelder
<ned at nedbatchelder.com> wrote:

>On 11/22/14 9:47 PM, Seymore4Head wrote:
>> What do I need to do to make a and b have different values?
>> import random
>> class RPS:
>>      throw=random.randrange(3)
>> a=RPS
>> b=RPS
>
>This simply makes a and b into other names for the class RPS. To 
>instantiate a class to make an object, you have to call it:
>
>     a = RPS()
>     b = RPS()
>
>> ---------
>> I tried:
>>
>> class RPS:
>>      def __init__(self):
>>          self.throw=random.randrange(3)
>>
>> AttributeError: type object 'RPS' has no attribute 'throw'
>
>This is the right way to define the class (almost: in Py 2, you should 
>derive from object). Once you make an object from it, you will have what 
>you want:
>
>     class RPS(object):
>         def __init__(self):
>             self.throw = random.randrange(3)
>
>     a = RPS()
>     b = RPS()
>     print a.throw
>     print b.throw

Now I am trying to add a dictionary, but it is broke too.

How do I fix:
class RPS:
    key={0:"rock", 1:"paper",2:"scissors"};
    def __init__(self):
        self.throw=random.randrange(3)
        self.key=key[self.throw]



More information about the Python-list mailing list