Created
June 26, 2017 20:39
-
-
Save Double-A-92/e5571f8e8bf36a565032ab8186b0360f to your computer and use it in GitHub Desktop.
Refactoring for https://www.reddit.com/r/learnprogramming/comments/6jkljq/beginner_need_little_help_with_dice_script/
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 | |
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