Skip to content

Instantly share code, notes, and snippets.

@Double-A-92
Created June 26, 2017 20:39
Show Gist options
  • Save Double-A-92/e5571f8e8bf36a565032ab8186b0360f to your computer and use it in GitHub Desktop.
Save Double-A-92/e5571f8e8bf36a565032ab8186b0360f to your computer and use it in GitHub Desktop.
import random
from msvcrt import getch
class Die:
def __init__(self, id):
self.value = 1
self.id = id
def roll(self):
self.value = random.randint(1, 6)
def __repr__(self):
return 'Dice #{}: {}'.format(self.id, self.value)
def roll_all_dice():
dice = [Die(i) for i in range(5)]
for die in dice:
die.roll()
print(die)
def user_wants_to_stop():
print("Press enter to roll again (and again(aaand again)) or just hit esc to quit!")
key = 0
while key not in [13, 27]: # Until user hits Enter or Esc
key = ord(getch())
return key == 27 # User hit Esc
def main():
while True:
roll_all_dice()
if user_wants_to_stop():
exit()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment