简体   繁体   中英

Class function not taking correct parameters Python

I am writing a code to simulate roulette games and have created a class that runs multiple simulations using one class function. I also have another class function that is used to get statistics from the simulation. In order to run the stats function I must call the simulation function. Here is the code,

class SingleBet:
    
    def __init__(self,bet):
        self.bet = bet
        self.payout = payout(bet) # this is initialized in a different function but works great
        
    """Simulates games until either money reaches disired multiple of start value is reached or you go bankrupt. 
    Returns probability of of reaching winning total""" 
    def play_to_mult(self,wager,mult,start=100,iters=1000):
        win = 0
        wager = wager
        mult = mult
        print('wager',wager)
        print(start)
        print(start*mult)
        for i in range(iters):
            money = start
            while wager <= money <= (start*mult): #requires enough money to place bet
                roll = rd.randint(1,38)
                if roll in self.bet:
                    money += self.payout*min(wager,money) #Makes sure you cannot win more than you wager
                else:
                    money -= wager
            if money>= start*mult:
                win+=1
        probwin = 100*win/(iters)
        return probwin
    
    """Uses play_to_mult function and uses returned value to calculated expectation value of game and standard deviation"""
    def stats(self,wager,mult,start=100):
        probwin = SingleBet.play_to_mult(self,mult,start)
        mu = probwin*start*mult
        sigma = np.sqrt((probwin*(start*mult)**2)-mu**2)

I have an instance of this class called odds and when I run the following code everything works as expected.

odds = SingleBet(odd) #where odd is just a list of numbers
odds.play_to_mult(20,1.1)

But when I run the following code the 'wager' parameter is assigned the value of 1.1 and the value of 20 I send in just vanishes.

odds.stats(20,1.1)

What would be causing this issue. Why does the play_to_mult function work great when called outside the class, but gives me issues when called from within?

Call self.play_to_mult(self,mult,start) instead of SingleBet.play_to_mult(self,mult,start) .

After some messing around and even trying calling it as self.play_to_mult() I found things still weren't working but I was able to fix the issues by making the methods class methods using the @classmethod decorator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM