You are required to design and implement a “Dice Roller” program that allows the user to simulate rolling dice. The user is prompted to enter their desired roll in “[quantity]d[sides]” format, e.g. “4d6” would roll four six-sided dice and “2d10” would roll two ten-sided dice. After each roll, some statistics are displayed and the user is prompted to enter another roll. The user may enter “x” to exit the program, or “h” to view some instructions. An advanced feature of the program is to allow the user to perform rolls with “exploding dice”, indicated by adding a “!” to the end of the roll, e.g. “4d6!” This is detailed later in this document. Details of the program requirements can be found in the “Program Requirements” section.
from random import randint
hlp = "enter desired roll in “[quantity]d[sides]” format"
hlp += "\n“h” to show help message"
hlp += "\n“x” to end the program"
while True:
comand = input("enter desired roll:\n")
if comand == "x":
print("bye!")
break
elif comand == "h":
print(hlp)
continue
else:
try:
q,s = comand.split("d")
q,s = int(q), int(s)
except:
print(hlp)
continue
statistic = {}
for i in range(q):
r = randint(1,s)
if r in statistic:
statistic[r] += 1
else:
statistic[r] = 1
print(f"after {q} rolls of a {s}-sided die, the following values are obtained:")
for value in statistic:
print(f"side {value} appeared {statistic[value]} times")
Comments
Leave a comment