Answer to Question #326272 in Python for kikka

Question #326272

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.


1
Expert's answer
2022-04-09T17:54:04-0400
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")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS