""" craps.py rules: roll two dice if 2, 3, 12 => lose if 7, 11 => win otherwise result is "point" then continue to roll if get point you win if 7 => lose otherwise keep rolling Running it looks like this: $ python craps.py -- craps odds -- You won 49493 games out of 100000 so your odds are winning are 0.49493 In class coding exercise | Oct 2018 | cs.marlboro.college """ from random import randint DEBUG = False def print_debug(message): if DEBUG: print("debug: ", message) def roll(n_dice=2, sides=6): """ return sum of two dice """ total = 0 for i in range(n_dice): total = total + randint(1, sides) return total def win_at_craps(): """ simulate craps; return True win """ print_debug("-- win_at_craps() --") first_roll = roll() print_debug("Your first roll is {}".format(first_roll)) if first_roll in (7, 11): print_debug("You win!") return True elif first_roll in (2, 3, 12): print_debug("You lose! (sucker...)") return False else: while True: next_roll = roll() print_debug("next roll is {}".format(next_roll)) if next_roll == first_roll: print_debug("You win (finally)") return True elif next_roll == 7: print_debug("You lose (finally)") return False def main(): print("-- craps odds --") wins = 0 n_games = 100000 for i in range(n_games): if win_at_craps(): wins = wins + 1 print("You won ", wins, " games out of ", n_games, \ " so your odds are winning are ", wins/n_games) if __name__ == '__main__': import doctest doctest.testmod() main()