0

The code below is part of a simple craps game that I am writing for practice. I have the dice rolling down, but I can't get this betting command to work out. The only module that I am really interrested here is the bet1(x) module but I copied the entire test code so that somebody could see exactly what I was trying to do.

Right now I'm just trying to make sure people can't enter invalid amounts to bet: no less than zero and no more than they have in the bank. A value in this range should end the loop and store the bet variable, but the loop doesn't break when I enter a number in the desired range. I've been tinkering with this for an embarrasingly long time now and I still can't get it to work.

This is my first post so I apologize if I'm missing anything or do this incorrectly.

Help please?

def betting():
    

    global bank; bank = 500   
    global bet; bet = 0


   
# defines the betting process within the program                     
    def bet1(x):
#set bet loop
        bet_loop=0
#loop the bet process until the bet is valid
        while bet_loop == 0:
#tell how much money you have        
            print "You have", x, "dollars in the bank, how much do you bet?"
#input b            
            bet = raw_input("Wager?") 
#test to make sure that b and x stayed (i did this because it won't work.)            
            print bet
            print x
#checks to see if your bet is valid   
            if bet  == "quit":
                break
            elif bet <= 0 or bet > x:
                print "Invalid bet."
            else:
                break 
                


#runs the defined function with the value x set as the global value of bank                        
    bet1(bank)
#another test to make sure its all working.
    print bank
    print bet
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by woooee
0

You will have to identify the global variables, bet et al. Otherwise, you will be using a second block of memory that is also named bet but local to the function.

if bet  == "quit":
                break
            elif float(bet) <= 0 or float(bet) > x:
                print "Invalid bet."
            else:
                print x, "is a valid amount to bet"
                ##    This would also work
                bet_loop=1
                break

This works much better as a class.

class Betting:
    def __init__(self):
       self.bank = 500.00
       self.bet = 0.00
   
    # defines the betting process within the program
    def bet1(self):
        #set bet loop
        bet_loop=0
        #loop the bet process until the bet is valid
        while bet_loop == 0:
            #tell how much money you have        
            print "You have", self.bank, "dollars in the bank, how much do you bet?"
            #input b            
            self.bet = raw_input("Wager?") 
            #test to make sure that b and x stayed (i did this because it won't work.)            
            print "self.bet in class =", self.bet
            print "self.bank in class=", self.bank
            #checks to see if your bet is valid   
            if self.bet  == "quit":
                break
            elif float(self.bet) <= 0 or float(self.bet) > self.bank:
                print "Invalid bet."
            else:
                print self.bet, "is a valid amount to bet"
                print "if you win you will have %7.2f dollars" % (float(self.bet) +self.bank)
##                bet_loop=1     ## another option
                break 

##=====================================================================
if __name__ == "__main__":
   ## create an instance of the class
   BB=Betting()     ## or instantiate the class (an awful word)
                    ## when you destroy the class do you outstantiate?
   BB.bet1()     ## run this function in the class
   #another test to make sure its all working.
   print "bank =", BB.bank
   print "bet =", BB.bet
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.