[Tutor] Blackjack Betting

Prasad, Ramit ramit.prasad at jpmchase.com
Thu Jun 30 21:52:47 CEST 2011


>I keep getting the error: “NameError: global
>name 'bet' is not defined.” I know I came across this error before in a
>previous thread, but I am confused on how to solve this, since I am also
>trying to juggle it with inherited methods at the same time.

Telling us this without the stack trace is pretty unhelpful. The Python exceptions include a very good stack trace (with line numbers +- 1 line) and a decent description. This error means that wherever this error was raised was a reference to the name "bet" and it had no reference to any "bet" at that point. The reasons could be that you forgot to assign a value to the name first, maybe it was a typo, in a preceding function, forgot the reference to self, etc, etc.

I took a *quick* look at the code and found this (I did not check for other problems or if this error happens in more than one place):


class BJ_Player(BJ_Hand, Bet):
    """ A Blackjack Player. """
    def is_hitting(self):
        response = games.ask_yes_no("\n" + self.name + ", do you want a hit? (Y/N): ")
        return response == "y"

    def bust(self):
        print(self.name, "busts.")
        self.lose()

    def lose(self):
        print(self.name, "loses.")
        betting = Bet()
        bet.stash -= bet.wager

    def win(self):
        print(self.name, "wins.")
        bet = Bet()
        bet.stash += bet.wager

    def push(self):
        print(self.name, "pushes.")


There are a couple things wrong with this class. First, you never define an __init__ which might be technically okay but is unlikely to be what you want for any user defined class (especially one with multiple inheritance). Since there is no __init__ defined, it will call the first parent class BJ_Hand.__init__ but not the second parent class Bet.__init__ (if it has one). 

Next for BJ_Player.lose(), bet is never defined and thus neither is bet.stash. Maybe you meant betting.stash -= betting.wager? I bet if you ran lint/pylint on this module it would have told you the error without even having to run it.


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423



-----Original Message-----
From: tutor-bounces+ramit.prasad=jpmchase.com at python.org [mailto:tutor-bounces+ramit.prasad=jpmchase.com at python.org] On Behalf Of Vincent Balmori
Sent: Thursday, June 30, 2011 1:49 PM
To: tutor at python.org
Subject: [Tutor] Blackjack Betting


I have been working on another challenge that involves improving the
Blackjack program so the players can wager an amount. If a player loses they
will be removed from the game.  I keep getting the error: “NameError: global
name 'bet' is not defined.” I know I came across this error before in a
previous thread, but I am confused on how to solve this, since I am also
trying to juggle it with inherited methods at the same time. Here is the old
and new file for the blackjack program for comparison:

http://old.nabble.com/file/p31966195/blackjack.py blackjack.py 
http://old.nabble.com/file/p31966195/blackjackbetting.py blackjackbetting.py 
-- 
View this message in context: http://old.nabble.com/Blackjack-Betting-tp31966195p31966195.html
Sent from the Python - tutor mailing list archive at Nabble.com.

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase & Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase &
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.


More information about the Tutor mailing list