Python beginner - red/black roulette program question

Hello,

I knew BASIC back in the 80’s… and figured it would be nice to start reading about Python and give it a go as a new hobby. I decided to try to write a red/black roulette betting program. For the heck of it I’m just going up in bets by power of 2, (up to 64). Like typical martingale betting, wins go back to the original bet amount, losses stop at 64x.

I have it working, (I’ll include my crude coding below). I have also set it up to double the betting amounts if and when the bankroll triples. What I CAN NOT figure out is this:

I want to incorporate ‘dialing down’ the bets if the bankroll falls below the prior doubling amount, until (and if) it gets back over that same threshold, at which point I’d like the bets go go back to the double amount cycle they were at. And so on.

I’d be very grateful for any help with this. I’ve spent too much time trying and figured it was time to seek some expert help. Thanks very much in advance for any assistance.

OK - the code:

import random

Bankroll is the starting bankroll

bankroll = 50

x is a bankroll counter that will double the bet if and when the bankroll triples

x = 50

s is a counter for each spin of the wheel

s = 0

wl = 1

wl (win or loss) is the bet. First bet is 1.

wheel = [“Red”] * 18 + [“Black”] * 18 + [“Green”] * 1

bankroll_history =

This below is to double the bets if / when the bankroll triples as the program runs

while bankroll > 0:

if bankroll/3 >= x:
    
    wl *= 2
    
    x = bankroll
    
    print ("BETS DOUBLED AT", x)
    
else:
                
    
    spin = random.choice(wheel)
    
    if spin == "Red":
        bankroll += wl
        s+=1
        print (s,"WIN  ",    wl, "TOTAL = ", bankroll)
        bankroll_history.append(bankroll)
    else:
        bankroll -= wl
        s+=1
        print (s,"Lose ",   wl, "TOTAL = ", bankroll)
        if bankroll < 0: break
        
        spin = random.choice(wheel)
        
        if spin == "Red":
            bankroll += (wl*2)
            s+=1
            print (s,"WIN  ",   wl*2, "TOTAL = ", bankroll) 
            bankroll_history.append(bankroll)
        else: 
            bankroll -= (wl*2)
            s+=1
            print (s,"Lose ",  wl*2, "TOTAL = ", bankroll)
            if bankroll < 0: break
            
            spin = random.choice(wheel)
            
            if spin == "Red":
                bankroll += (wl*4)
                s+=1
                print (s,"WIN  ",   wl*4, "TOTAL = ", bankroll) 
                bankroll_history.append(bankroll)  
            else: 
                bankroll -= (wl*4)
                s+=1
                print (s,"Lose ",  wl*4, "TOTAL = ", bankroll) 
                if bankroll < 0: break
                
                spin = random.choice(wheel)
            
                if spin == "Red":
                    bankroll += (wl*8)
                    s+=1
                    print (s,"WIN  ",   wl*8, "TOTAL = ", bankroll) 
                    bankroll_history.append(bankroll)  
                else: 
                    bankroll -= (wl*8)
                    s+=1
                    print (s,"Lose ",  wl*8, "TOTAL = ", bankroll)
                    if bankroll < 0: break
                    
                    spin = random.choice(wheel)
            
                    if spin == "Red":
                        bankroll += wl*16
                        s+=1
                        print (s,"WIN  ",   wl*16, "TOTAL = ", bankroll) 
                        bankroll_history.append(bankroll)  
                    else: 
                        bankroll -= (wl*16)
                        s+=1
                        print (s,"Lose ",  wl*16, "TOTAL = ", bankroll)
                        if bankroll < 0: break
                        
                        spin = random.choice(wheel)
            
                        if spin == "Red":
                            bankroll += (wl*32)
                            s+=1
                            print (s,"WIN  ",   wl*32, "TOTAL = ", bankroll) 
                            bankroll_history.append(bankroll)  
                        else: 
                            bankroll -= (wl*32)
                            s+=1
                            print (s,"Lose ",  wl*32, "TOTAL = ", bankroll)
                            if bankroll < 0: break
                            
                            spin = random.choice(wheel)
                            
                            if spin == "Red":
                                bankroll += (wl*64)
                                s+=1
                                print (s,"WIN  ",   wl*64, "TOTAL = ", bankroll) 
                                bankroll_history.append(bankroll)  
                            else: 
                                bankroll -= (wl*64)
                                s+=1
                                print (s,"Lose ",  wl*64, "TOTAL = ", bankroll)
                                if bankroll < 0: break

bankroll_history.append(bankroll)

Welcome.

Excellent choice of language. It is indeed as much fun as BASIC was in the '80s but a much better language.

When you post code, please follow this advice. You can edit the fences into your first post (I think) until it looks right. You’ll get more help if it is easy to read.

This is very repetitive code, so my first thought is that it would be worth learning how to define and use a function.

Now, looking at the logic, I can see that what drives this is the number of consecutive wins and whether the bankroll has hit zero. On a win you continue deeper into the nested if statements with a higher stake and on a loss you go back to base or break out of the loop if ruined. (Should that be <= 0 not < 0?)

I think that instead of nesting the ifs like this you could keep a state variable that keeps track of the number of consecutive wins, is used to decide the next stake, then you spin, and have only one if-else that either increments or zeros the consecutive wins.

That approach then gives you the flexibility (I think) for a more complicated rule about how you increase or decrease the stake based on the state, which I understand you to be looking for. (I’m not au fait with gambling terminology in general.) Maybe you keep track of some other state, like number of consecutive losses.

As it is, part of your state is represented by “where I am in the program”. This is a time-honoured technique in many fields, but here it means you would have to spread your decision-making all over the code.

1 Like

Hi Jeff,

Thanks very much for the informative reply. I’m glad I picked the right language. I have a nephew that works at Cisco as a programmer and he recommended Python.

Yes, as you mentioned, that break should be <= 0, thanks.

The reason I made the nested if’s was because I am considering other (non-binary) increases and decreases of the bet amount over the cycle of however many bets. (What I posted was a simpler power of 2 example).

Actually, the way it’s set up, on a win it goes back to base, on a loss it continues deeper into the nested if’s, until a win (back to base) or until the cycle of losses is over, then back to base. (Or break at <=0).

So, when I tried to incorporate a halving of the bet if it falls below the previous bankroll of the doubling of bets, I would see another halving after subsequent losses if the bankroll was still under that prior doubling threshold. The program would eventually wind down with decreasing betting amounts approaching zero. I tried to come up with a variable that would track the prior doubling threshold through the nested if’s but I haven’t found it yet. The variable needs to remember the bankroll amount that doubled the bet, and if the bankroll is lower than that threshold, halve the bet and go back to base - but only halve that betting amount ONCE while bets continue to be lower than the doubling threshold. I can’t seem to find a way to avoid continually halving bets below the threshold.

Thanks again for your reply. I’ll keep working on this, and would welcome any other insights from you and others. I’ll also read the link you recommended and post the code to be more easily read.

Threshold counter (x)
I’m thinking, after any loss in any of the nested betting if’s goes below the previous doubling threshold (x) the betting amount (wl) is halved, and x needs to be reset to the old x, then go back to base. Since x starts at 0, I think I need another variable that stays at zero in the beginning. I’ll try to put that into each loss “if”. Yes, a new variable that tracks old x. We’ll see…

Hi,

what @jeff5 recommended is on point. You could probably more than halve your total code by this single recommendation. Learning how to apply functions to your code will also greatly help in simplifying it.

One potential function may be (you will have to test it yourself and modify it to verify functionality - note that I am assuming that the only input variable to the function is w1 since it appears to be the only independent variable in the test script here and all others are dependent and global in scope - name function to your preference):

def my_func(w1):

    global bankroll, s, bankroll_history 

    bankroll += (wl*2)
    s+=1
    print (s,"WIN  ",   wl*2, "TOTAL = ", bankroll) 
    bankroll_history.append(bankroll)  

In code, you would call it as:

my_func(w1)

You can then use this function (or similar) under the appropriate if/else statements in your script. Not only will it have the potential of reducing your total code footprint by more than half, but it will also make it that much easier for debugging.

1 Like