forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diceV2_dynamic.py
82 lines (60 loc) · 2.21 KB
/
diceV2_dynamic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 or someone "
"has tweaked with code they weren't supposed to touch!")
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, question_string, lower_limit):
while True:
item = input(question_string)
item = checkInput(item)
if type(item) == int:
if item <= lower_limit:
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 # 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)
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)