![]() |
Dice Roller - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Dice Roller (/thread-8028.html) |
Dice Roller - mcmxl22 - Feb-03-2018 import random def dice(): sides = [1, 2, 3, 4, 5, 6] rollDice = input('roll? ') if rollDice == 'y': i = random.choice(sides) print(i) dice() elif rollDice == 'n': quit() else: print('invalid entry') dice() dice() RE: Dice Roller - Windspar - Feb-04-2018 dice roller ? import random def dice(): sides = 6 count = 1 while True: roll_dice = input('roll ? >> ') if roll_dice in ['y', '']: line = '' for i in range(count): line += str(random.choice(range(1, sides + 1))) + " " print(line) elif roll_dice.startswith('sides'): sides = int(roll_dice[6:]) print('using a {0} sided dice'.format(sides)) elif roll_dice.startswith('count'): count = int(roll_dice[6:]) print('using {0} dice'.format(count)) elif roll_dice == 'n': break else: print('invalid entry') dice() RE: Dice Roller - mcmxl22 - Feb-04-2018 @Windspar, I like to keep it simple. RE: Dice Roller - Windspar - Feb-05-2018 Only problem I see with your code. Is those nasty recursive calls. Using loops is a better solution. import random def dice(): while True: roll_dice = input('roll ? >> ') if roll_dice in ['y', '']: print(random.choice(range(1, 7))) elif roll_dice == 'n': break else: print('invalid entry') dice() RE: Dice Roller - buran - Feb-05-2018 instead of print(random.choice(range(1, 7))) keep it simple and better use print(random.randint(1, 6)) https://docs.python.org/3/library/random.html#functions-for-integers |