On Fri, Jul 1, 2011 at 7:11 PM, David Merrick <merrick...@gmail.com> wrote:
> NameError: global name 'stash' is not defined > > There are a _lot_ of missing pieces here; I think you started writing code without a plan of how your game would actually run. The current error is only the first you're going to run into here... That being said, we might as well fix the first one. You either have several problems here, or just one; I'm going to go for the "just one" option: I'm going to assume that you actually want "stash" to be an attribute of a Bet object - I think that it should be an attribute of each player, not of their individual bets, but that's another issue. The only time you can refer to a variable without qualifying it is from within the same block of code. Otherwise, the interpreter has to guess which context (also known as "scope") the variable is supposed to belong to, and the interpreter refuses to guess. The error you're getting boils down to "You referred to a variable called 'stash', but since you didn't define it in the local scope, I assume it's a global variable - but you didn't define it there either! What gives?" In Bet.__init__(), you have > stash = money > This makes "stash" a local variable in the scope of __init__(), and as soon as __init__() terminates, so does "stash". What you want is: > self.stash = money > This makes "stash" an attribute of Bet. To refer to "stash" from inside the Bet object, you need to refer to it as "self.stash"; from outside, after you've created a Bet object ("bet = Bet()") you need to refer to it as "bet.stash". One more thing: you define Bet.__init__() as taking two parameters, "bet" and "money"; you make "money" optional by defaulting it to 10, but "bet" has no default, so if your program ever got around to executing "bet = Bet()" it would blow up because of a missing parameter. Also, your use of variable names is confusing. "bet = Bet()" is OK, but ALSO having "bet" as a parameter to Bet.__init__()? Python won't be confused, but I bet you will be - I know I am.
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor