Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81331
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.008 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'class,': 0.07; 'indices': 0.07; '__init__': 0.09; 'here?': 0.09; 'translate': 0.10; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'posted': 0.15; "(it's": 0.16; '23,': 0.16; 'defined,': 0.16; 'earlier.': 0.16; 'expecting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nameerror:': 0.16; 'once.': 0.16; 'sequential': 0.16; 'subject:class': 0.16; 'throw': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; '(but': 0.19; "python's": 0.19; 'seems': 0.21; 'import': 0.22; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'fairly': 0.24; 'helpful': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'script': 0.25; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'included': 0.31; 'code': 0.31; 'lines': 0.31; 'question:': 0.31; 'anyone': 0.31; 'file': 0.32; 'class': 0.32; 'thanks!': 0.32; 'another': 0.32; 'says': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'useful': 0.36; 'list': 0.37; 'thank': 0.38; 'nov': 0.38; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; 'recent': 0.39; 'explain': 0.39; 'how': 0.40; 'even': 0.60; 'referred': 0.60; 'tell': 0.60; 'numbers': 0.61; 'information,': 0.61; "you're": 0.61; 'first': 0.61; 'information': 0.63; 'name': 0.63; 'telling': 0.64; 'more': 0.64; 'skip:1 20': 0.65; 'helping': 0.70; 'cut': 0.74; 'subject:have': 0.80; 'you:': 0.81; '"it': 0.84; 'all!': 0.84; 'armed': 0.91; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=5KljoaIORh22sjQZ5/TOO1E+cJKmKwS0T5/eHAbNJWg=; b=MtciYQFAcwxJuMNUTLcOaqNRzcjHLc34fDgGzl0eEk8zfpMMxp2UdYcNrp+Fi0lr4t wlFO0l+i33xp/OfMYD0udU3apHpE5iu4Z460rTeAMXTqJvAA7Qgve1PeJIcbE8of84bY t3Y7Gu6wgScMmjCTDymcpdM4GB9d4MTtYcoJ21keRdZ5NbNEIcRJMbEhPk1xwtrUOLE2 TFrAmHtWqIMQAKlsZm1Xpeyb+GGTvzrMvL/jhxCF7TFIuRv1n5tll1TffBoQlfHXfRVC Sgu3ApLwlVc/SoeWHhFIFCExrOeJvQkgGbW81duMYChPAz4gplIySptyXjlw/7CNDfMy UZww== |
| MIME-Version | 1.0 |
| X-Received | by 10.50.171.227 with SMTP id ax3mr4530869igc.0.1416722409038; Sat, 22 Nov 2014 22:00:09 -0800 (PST) |
| In-Reply-To | <2qn27adjq412n9nff9kv5jh25d1ekkbncp@4ax.com> |
| 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 | Sun, 23 Nov 2014 17:00:08 +1100 |
| Subject | Re: I have no class |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16160.1416722412.18130.python-list@python.org> (permalink) |
| Lines | 62 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1416722412 news.xs4all.nl 2837 [2001:888:2000:d::a6]:56490 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81331 |
Show key headers only | 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
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