Created
February 5, 2019 15:33
-
-
Save davegotz/9ec114e8c18cd766a421c696a5474a45 to your computer and use it in GitHub Desktop.
Rolling dice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# Returns a random value from 1-6 with uniform distribution. | |
def roll(): | |
return random.randint(1, 6) | |
# Simulates a pair of dice | |
def roll_dice(): | |
# Roll two dice, return the total. | |
total = roll() + roll() | |
print(total) | |
return total | |
# The main function which will roll dice until we have the same value | |
# two times in a row. | |
def main(): | |
# Roll the first time. | |
current_roll = roll_dice() | |
previous_roll = None | |
count = 1 | |
# Roll dice over and over until I have the same value come up twice in a row. | |
while current_roll != previous_roll: | |
# Roll again. | |
previous_roll = current_roll | |
current_roll = roll_dice() | |
count += 1 | |
print("You rolled", count, "times.") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment