Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- def make_fair_dice(sides):
- """Return a die that returns 1 to SIDES with equal chance."""
- assert type(sides) == int and sides >= 1, 'Illegal value for sides'
- def dice():
- return randint(1,sides)
- return dice
- four_sided_dice = make_fair_dice(4)
- six_sided_dice = make_fair_dice(6)
- def roll_dice(num_rolls, dice=six_sided_dice, who='Boss Hogg'):
- """Calculate WHO's turn score after rolling DICE for NUM_ROLLS times.
- num_rolls: The number of dice rolls that will be made; at least 1.
- dice: A function of no args and returns an integer outcome.
- who: Name of the current player, for commentary.
- """
- assert type(num_rolls) == int, 'num_rolls must be an integer.'
- assert num_rolls > 0, 'Must roll at least once.'
- k, total = 1, 0
- while k <= num_rolls:
- k += 1
- x = dice()
- print(who, "rolled a", x)
- if x == 1:
- total = 1
- elif total == 1:
- total = 1 + 0
- else:
- total = total + x
- return total
- roll_dice(4)
Add Comment
Please, Sign In to add comment