""" craps.py Find the odds of winning at craps. craps: a) first roll 2 dice b) if sum of dice is 2, 3, or 12 => lose c) if sum of dice is 7 or 11 => win d) otherwise, the value is the "point" e) following rolls f) if sum is 7 => lose e) if sum is "point" => win g) keep rolling Class exercise for intro python. Jim Mahoney | March 2018 """ import random def two_dice(): """ return sum of rolling two dice """ return random.randint(1,6) + random.randint(1,6) def win_craps(verbose=False): """ return True if we win one game of craps """ first_roll = two_dice() if verbose: print("") print("- craps -") print("first roll is {}".format(first_roll)) if first_roll in (7, 11): if verbose: print("1st win!") return True elif first_roll in (2, 3, 12): if verbose: print("1st lose!") return False else: while True: next_roll = two_dice() if verbose: print("next roll is {}".format(next_roll)) if next_roll == 7: if verbose: print("lose!") return False elif next_roll == first_roll: if verbose: print("win!") return True def count_wins(plays = 1000000): print("Playing craps {} times.".format(plays)) count = 0 for i in range(plays): if win_craps(): count = count + 1 print("Number of wins = {}.".format(count)) print("Odds of winning are {}.".format(float(count)/plays)) count_wins()