I have no class

Seymore4Head Seymore4Head at Hotmail.invalid
Sat Nov 22 23:15:36 EST 2014


On Sat, 22 Nov 2014 22:52:33 -0500, Joel Goldstick
<joel.goldstick at gmail.com> wrote:

>On Sat, Nov 22, 2014 at 10:35 PM, Seymore4Head
><Seymore4Head at hotmail.invalid> wrote:
>> 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]
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>can you please show traceback and output?
Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
7, in <module>
    a=RPS()
  File "C:\Documents and Settings\Administrator\Desktop\rps.py", line
6, in __init__
    self.key=key[self.throw]
NameError: name 'key' is not defined



More information about the Python-list mailing list