This is a second shot at creating a Martingale betting simulator. The original code needed such heavy refactoring that I just started from scratch.
Script simulates the Martingale betting strategy of betting a fixed amount until a loss occurs, at which point the bet is doubled to make up for the loss. This continues until a win occurs, after a win the bet is reset to the original bet value. I set the odds to mimic Blackjack (49% chance of a win). For simplicity, the amount won or lost in a round is equal to the bet. The simulation ends when the specified number of rounds has elapsed, the size of the next bet is larger than the current available funds, the available funds reach 0, or the goal profit is met.
I'm brand new to Python and coding in general, so any feedback would be appreciated.
import random
def main(rounds=10, bet=25, goal_profit=1000, each=False, end_script_prt=True):
"""Runs a simulation of the Martingale betting strategy over a specified number of rounds.
each = True will print a summary of each round's results. end_script_prt = True will
print a summary of the game's results"""
original_bet = bet
current_bet = bet
starting_funds = 5000
current_profit = 0
current_funds = starting_funds
round_n = 0
wins = 0
losses = 0
while current_profit < goal_profit\
and round_n < rounds\
and current_funds > 0\
and current_funds > current_bet:
round_n += 1
rng_v = rng()
current_funds += change_current_funds(current_bet, rng_v)
current_profit = current_profits(current_funds, starting_funds)
if winloss_generator(rng_v) == 'win':
wins += 1
elif winloss_generator(rng_v) == 'loss':
losses += 1
if each:
print('ROUND:', round_n, 'of', rounds)
print('BET:', current_bet)
print('OUTCOME:', winloss_generator(rng_v).capitalize())
print('WINS/LOSSES:', wins, 'Wins', losses, 'losses')
print('CURRENT FUNDS:', current_funds)
print('CURRENT PROFIT:', current_profit)
print()
current_bet = change_bet(original_bet, current_bet, rng_v)
print()
if end_script_prt:
end_script(round_n, wins, losses, starting_funds, current_funds, goal_profit, current_profit)
return change_iterated_winloss(current_profit, goal_profit)
def rng():
"""Returns random number"""
return random.random()
def winloss_generator(rng_v):
"""Returns win/loss condition"""
if rng_v <= .49:
return 'win'
if rng_v > .49:
return 'loss'
def change_current_funds(current_bet, rng_v):
"""Returns change in funds resulting from round outcome"""
if winloss_generator(rng_v) == 'win':
return current_bet
if winloss_generator(rng_v) == 'loss':
return current_bet * -1
def change_bet(original_bet, current_bet, rng_v):
"""Returns updated bet value
If outcome is a win, bet is reset to original value. If outcome is a loss bet is doubled"""
if winloss_generator(rng_v) == 'win':
return original_bet
if winloss_generator(rng_v) == 'loss':
return current_bet * 2
def current_profits(current_funds, starting_funds):
"""Returns current profit"""
return current_funds - starting_funds
def end_script(round_n, wins, losses, starting_funds, current_funds, goal_profit, current_profit):
"""Prints final outcome of the game and summary of the results"""
print('*************************')
if current_profit >= goal_profit:
print('YOU WIN!')
else:
print('YOU LOSE')
print('TOTAL ROUNDS:', round_n)
print('WIN/LOSS RECORD:', wins, 'Wins', losses, 'Losses')
print('STARTING FUNDS:', starting_funds)
print('ENDING FUNDS: ', current_funds)
print('GOAL PROFIT:', goal_profit)
print('ENDING PROFIT:', current_profit)
def change_iterated_winloss(current_profit, goal_profit):
"""Returns game's win/loss outcome as a string"""
if current_profit >= goal_profit:
w_l = 'w'
return w_l
else:
w_l = 'l'
return w_l
def iterated_winloss_count(iterations, each=False):
"""Returns a summary of the total win/loss record across game iterations. each = True will
print each game result individually"""
total_wins = 0
total_losses = 0
for x in range(iterations):
game_outcome = main(1000, 25, 5000, False, False)
if game_outcome == 'w':
total_wins += 1
if each:
print('WIN!')
if game_outcome == 'l':
total_losses += 1
if each:
print('LOSS')
print()
print('WINS/LOSSES OVER', iterations, 'ITERATIONS:', total_wins, 'Wins', total_losses, 'Losses')
# Single Game
if True:
main(1000, 25, 5000, True, True)
# Iterated Games
if False:
iterated_winloss_count(1000, False)