Guest User

Untitled

a guest
Nov 16th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. from random import randint
  2.  
  3. def make_fair_dice(sides):
  4. """Return a die that returns 1 to SIDES with equal chance."""
  5. assert type(sides) == int and sides >= 1, 'Illegal value for sides'
  6. def dice():
  7. return randint(1,sides)
  8. return dice
  9.  
  10. four_sided_dice = make_fair_dice(4)
  11. six_sided_dice = make_fair_dice(6)
  12.  
  13. def roll_dice(num_rolls, dice=six_sided_dice, who='Boss Hogg'):
  14. """Calculate WHO's turn score after rolling DICE for NUM_ROLLS times.
  15.  
  16. num_rolls: The number of dice rolls that will be made; at least 1.
  17. dice: A function of no args and returns an integer outcome.
  18. who: Name of the current player, for commentary.
  19. """
  20. assert type(num_rolls) == int, 'num_rolls must be an integer.'
  21. assert num_rolls > 0, 'Must roll at least once.'
  22. k, total = 1, 0
  23. while k <= num_rolls:
  24. k += 1
  25. x = dice()
  26. print(who, "rolled a", x)
  27. if x == 1:
  28. total = 1
  29. elif total == 1:
  30. total = 1 + 0
  31. else:
  32. total = total + x
  33. return total
  34. roll_dice(4)
Add Comment
Please, Sign In to add comment