From fdae317ff44823f1ffb01005194ab9a1e29d7d2d Mon Sep 17 00:00:00 2001 From: DevDoggo Date: Wed, 22 Nov 2017 01:10:34 +0100 Subject: [PATCH 1/2] Re-made the dice.py code into a whole new dice-program. This program is more dynamic in the sense that you can change sides and amounts of dices in real-time. You can specify an infinite amount of sides and dices, however you do have a certain lower-limit to sides and amounts of dices to 'realistically' represent the dices in a 3-dimensional space. --- diceV2_dynamic.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 diceV2_dynamic.py diff --git a/diceV2_dynamic.py b/diceV2_dynamic.py new file mode 100644 index 00000000000..ff22b25b49b --- /dev/null +++ b/diceV2_dynamic.py @@ -0,0 +1,84 @@ + + + + +import random +#Class that that holds dice-functions. You can set the amount of sides and roll with each dice object. +class Dice(): + def __init__(self): + self.sideCount=6 + + def setSides(self, sides): + if sides > 3: + self.sides = sides + else: + print("This absolutely shouldn't ever happen. The programmer sucks.") + + def roll(self): + return random.randint(1, self.sides) + + +###===================================================================== + + +#Checks to make sure that the input is actually an integer. +#This implementation can be improved greatly of course. +def checkInput(sides): + try: + if int(sides) != 0: + if (float(sides)%int(sides) == 0): #excludes the possibility of inputted floats being rounded. + return int(sides) + else: + return int(sides) + + except: + print("Invalid input!") + return None + + +#Picks a number that is at least of a certain size. +#That means in this program, the dices being possible to use in 3 dimensional space. +def pickNumber(item, questionString, lowerlimit): + while True: + item = input(questionString) + item = checkInput(item) + if type(item) == int: + if item <= lowerlimit: + print("Input too low!") + continue + else: + return item + + +#Main-function of the program that sets up the dices for the user as they want them. +def getDices(): + dices = [] + sides = None + diceAmount = None + sideLowerLimit = 3 + diceLowerLimit = 1 + + sides = pickNumber(sides, "How many sides will the dices have?: ", sideLowerLimit) + diceAmount = pickNumber(diceAmount, "How many dices will do you want?: ", diceLowerLimit) + + for i in range(0, diceAmount): + d = Dice() + d.setSides(sides) + dices.append(d) + + return dices + + + +dices = getDices() +#================================================================= +#Output section. + + +rollOutput = "" + +for dice in dices: + rollOutput = rollOutput + str(dice.roll()) + ", " + +rollOutput = rollOutput[:-2] +print (rollOutput) From 04ea30cfb281f93f64f1697a3c45c3b0a5c950d6 Mon Sep 17 00:00:00 2001 From: Succ <33795198+DevDoggo@users.noreply.github.com> Date: Wed, 22 Nov 2017 01:27:51 +0100 Subject: [PATCH 2/2] Update diceV2_dynamic.py Just a quick add of comments I forgot the first time. --- diceV2_dynamic.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/diceV2_dynamic.py b/diceV2_dynamic.py index ff22b25b49b..2e0f373486a 100644 --- a/diceV2_dynamic.py +++ b/diceV2_dynamic.py @@ -12,7 +12,7 @@ def setSides(self, sides): if sides > 3: self.sides = sides else: - print("This absolutely shouldn't ever happen. The programmer sucks.") + print("This absolutely shouldn't ever happen. The programmer sucks or someone has tweaked with code they weren't supposed to touch!") def roll(self): return random.randint(1, self.sides) @@ -55,8 +55,8 @@ def getDices(): dices = [] sides = None diceAmount = None - sideLowerLimit = 3 - diceLowerLimit = 1 + sideLowerLimit = 3 #Do Not Touch! + diceLowerLimit = 1 #Do Not Touch! sides = pickNumber(sides, "How many sides will the dices have?: ", sideLowerLimit) diceAmount = pickNumber(diceAmount, "How many dices will do you want?: ", diceLowerLimit)