Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81331

Re: I have no class

References <e7h27ahs9rjaunu6d91opif62vbr2jefl9@4ax.com> <mailman.16156.1416712474.18130.python-list@python.org> <l9l27alml8scm4dvf29m2vidcudhh6766o@4ax.com> <mailman.16157.1416714761.18130.python-list@python.org> <2qn27adjq412n9nff9kv5jh25d1ekkbncp@4ax.com>
Date 2014-11-23 17:00 +1100
Subject Re: I have no class
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16160.1416722412.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Nov 23, 2014 at 3:15 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> 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

This information is far more helpful than "it is broke". Note how it
says that the name 'key' is not defined? You're expecting that it
would be defined, because you defined it a few lines earlier. So
here's a more useful way to ask the question:

-- cut --
I'm trying to use a dictionary to translate throw numbers into
descriptions, but even though the dictionary has been defined, Python
is telling me it hasn't.

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

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

It seems to me that 'key' is defined on line 3. Can anyone explain
what's happening here? Thanks!
-- cut --

Note that I've posted the *entire* code of your script (it's a
reconstruction, but I suspect it's fairly close; there might be more
code after that, but it's not affecting anything), and included the
traceback in the first message, rather than waiting for someone to ask
for it.

Armed with this information, someone can tell you:

1) Python's namespacing rules mean that 'key' is a part of the RPS
class, and can be referred to as 'self.key' or as 'RPS.key'
2) Use of 'self.key' for the textual form of the throw is shadowing
the first of those reference names (but it's a poor name anyway)
3) A list would work just as well as a dictionary here, since your
indices are sequential and start from zero
4) There's another function in the random module which can do both of
your steps at once.

Thank you for helping us help you help us all!

ChrisA

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 21:47 -0500
  Re: I have no class random832@fastmail.us - 2014-11-22 22:08 -0500
    Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:16 -0500
  Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-22 19:09 -0800
    Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:18 -0500
  Re: I have no class Ned Batchelder <ned@nedbatchelder.com> - 2014-11-22 22:14 -0500
    Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 22:35 -0500
      Re: I have no class Joel Goldstick <joel.goldstick@gmail.com> - 2014-11-22 22:52 -0500
        Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 23:15 -0500
          Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-23 17:00 +1100
            Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 05:52 -0500
              Re: I have no class Dave Angel <davea@davea.name> - 2014-11-23 10:16 -0500
                Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 10:54 -0500
                Re: I have no class Dave Angel <davea@davea.name> - 2014-11-23 11:14 -0500
                Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-23 11:49 -0500
                Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 03:59 +1100
                Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-23 09:02 -0800
                Re: I have no class Steven D'Aprano <steve@pearwood.info> - 2014-11-24 04:21 +0000
                Re: I have no class Dave Angel <davea@davea.name> - 2014-11-24 00:11 -0500
                Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 16:11 +1100
                Re: I have no class Dan Sommers <dan@tombstonezero.net> - 2014-11-24 13:44 +0000
                Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-25 00:48 +1100
                Re: I have no class Dave Angel <davea@davea.name> - 2014-11-24 00:20 -0500
                Re: I have no class Chris Angelico <rosuav@gmail.com> - 2014-11-24 16:21 +1100
                Re: I have no class Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-11-24 13:47 -0500
      Re: I have no class Rustom Mody <rustompmody@gmail.com> - 2014-11-22 19:55 -0800
        Re: I have no class Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-22 23:19 -0500
        Re: I have no class Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-11-23 15:35 +0000

csiph-web