|
| 1 | + |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import random |
| 6 | +#Class that that holds dice-functions. You can set the amount of sides and roll with each dice object. |
| 7 | +class Dice(): |
| 8 | + def __init__(self): |
| 9 | + self.sideCount=6 |
| 10 | + |
| 11 | + def setSides(self, sides): |
| 12 | + if sides > 3: |
| 13 | + self.sides = sides |
| 14 | + else: |
| 15 | + print("This absolutely shouldn't ever happen. The programmer sucks or someone has tweaked with code they weren't supposed to touch!") |
| 16 | + |
| 17 | + def roll(self): |
| 18 | + return random.randint(1, self.sides) |
| 19 | + |
| 20 | + |
| 21 | +###===================================================================== |
| 22 | + |
| 23 | + |
| 24 | +#Checks to make sure that the input is actually an integer. |
| 25 | +#This implementation can be improved greatly of course. |
| 26 | +def checkInput(sides): |
| 27 | + try: |
| 28 | + if int(sides) != 0: |
| 29 | + if (float(sides)%int(sides) == 0): #excludes the possibility of inputted floats being rounded. |
| 30 | + return int(sides) |
| 31 | + else: |
| 32 | + return int(sides) |
| 33 | + |
| 34 | + except: |
| 35 | + print("Invalid input!") |
| 36 | + return None |
| 37 | + |
| 38 | + |
| 39 | +#Picks a number that is at least of a certain size. |
| 40 | +#That means in this program, the dices being possible to use in 3 dimensional space. |
| 41 | +def pickNumber(item, questionString, lowerlimit): |
| 42 | + while True: |
| 43 | + item = input(questionString) |
| 44 | + item = checkInput(item) |
| 45 | + if type(item) == int: |
| 46 | + if item <= lowerlimit: |
| 47 | + print("Input too low!") |
| 48 | + continue |
| 49 | + else: |
| 50 | + return item |
| 51 | + |
| 52 | + |
| 53 | +#Main-function of the program that sets up the dices for the user as they want them. |
| 54 | +def getDices(): |
| 55 | + dices = [] |
| 56 | + sides = None |
| 57 | + diceAmount = None |
| 58 | + sideLowerLimit = 3 #Do Not Touch! |
| 59 | + diceLowerLimit = 1 #Do Not Touch! |
| 60 | + |
| 61 | + sides = pickNumber(sides, "How many sides will the dices have?: ", sideLowerLimit) |
| 62 | + diceAmount = pickNumber(diceAmount, "How many dices will do you want?: ", diceLowerLimit) |
| 63 | + |
| 64 | + for i in range(0, diceAmount): |
| 65 | + d = Dice() |
| 66 | + d.setSides(sides) |
| 67 | + dices.append(d) |
| 68 | + |
| 69 | + return dices |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +dices = getDices() |
| 74 | +#================================================================= |
| 75 | +#Output section. |
| 76 | + |
| 77 | + |
| 78 | +rollOutput = "" |
| 79 | + |
| 80 | +for dice in dices: |
| 81 | + rollOutput = rollOutput + str(dice.roll()) + ", " |
| 82 | + |
| 83 | +rollOutput = rollOutput[:-2] |
| 84 | +print (rollOutput) |
0 commit comments