[Tutor] OOP question

Nick Stinemates nstinemates at gmail.com
Tue Jan 18 18:52:26 CET 2011


Updated inline. Check the updated definiton of winAmount.

Nick

On Tue, Jan 18, 2011 at 9:25 AM, Ben Ganzfried <ben.ganzfried at gmail.com>wrote:

> Hey guys,
>
> I'm trying to get a version of Roulette working and I had a quick
> question.  Here is my code:
>
> class Outcome:
>
>     def __init__(self, name, odds):
>         self.name = name
>         self.odds = odds
>
>     def winAmount(self, amount):
>         return self.odds*amount
>
>     def __eq__(self, other):
>         if (self == other):
>             return True
>         else:
>             return False
>
>     def __ne__(self, other):
>         if (self != other):
>             return True
>         else:
>             return False
>
>     def __str__(self):
>         return "%s (%d:1)" % ( self.name, self.odds )
>
> Now whenever I try to call the winAmount method, I get the following error:
> NameError: global name 'odds' is not defined
>
> I'm wondering the following: I had thought that by initializing "odds" in
> the first initialization method that "odds" should then be accessible to all
> the other methods in the class.  This does not seem to be the case.  So what
> is the ideal fix?  On the one hand I still need to initialize "odds",
> right?  But if "odds" is within the initialization method, how else can it
> be accessed by the other methods?
>
> Thanks so much!
>
> Ben
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110118/1c5b1a8a/attachment.html>


More information about the Tutor mailing list