Quote (reading123 @ Mar 27 2022 09:08pm)
will donate FG to person who can resolve my query(s)
thank you
o i have a program i've been working on and i've been stuck on the final requirement, which ive listed below. to give you a bit of a background info on the program
It is a random dice generator - eg - 4d6 = 4 rolls of a 6 sided dice, and will calculate some misc statistics based on the rolls.
There is a function called 'exploding dice' = which means if you roll a max value, eg 6 on a 6 sided dice, it will 'explode' and you get a free reroll, which will continue until u stop rolling the max value.
I have attached my code and sorry its a bit messy
#Import random module
import random
#Function for heading.
def print_heading(text, width = 50):
print('=' * width)
print('|' + text.center(48) + '|')
print('=' * width)
#Call print_heading function.
print_heading('Welcome to Dice Simulator')
#Prompt user to enter a roll, "h" for help or "x" to exit.
roll_text = None
while roll_text not in ('h', 'x'):
roll_text = input('Enter a roll, "h" or "x": ').lower()
#If choice is h/H display help text.
if roll_text == "h":
print_heading('Help!')
print('Enter a roll in "[quantity]d[sides]" format.')
print('e.g. Enter 5d8 to roll five 8 sided die.\n')
print('Add ! to the end of the roll, e.g. 5d8!, to access the exploding dice function.')
print('Dice that roll the max value will be re-rolled and added.')
#If choice is x/X display goodbye heading and end loop.
elif roll_text == "x":
print_heading('Goodbye')
break
#Reassign roll_text to return true if ! is present at the end of initial use input.
explode =(roll_text.endswith('!'))
roll_text = roll_text[:-1]
#Validate roll entered, must be 4d6 format, first digit cannot be 0, second digit must be higher or equal to 2.
parts = roll_text.split('d')
if len(parts) == 2 and parts[0].isdigit() and parts[1].isdigit()and int(parts[0]) >=1 and int(parts[1]) >= 2:
#If roll is of a valid format, create variables for quantity and sides.
quantity = int(parts[0])
sides = int(parts[1])
#Display confirmation of roll.
print(f'Rolling {quantity}d{sides}...')
#Generate empty list to store rolls.
rolls = []
#Proceed with roll of dice - while quantity > 0 perform roll - display each result.
for results in range(quantity):
number = random.randint(0, sides)
print('You rolled..', number, '!')
rolls.append(number)
#While explode is true and a max roll is obtained, perform explode function.
while explode == True and number == sides:
print('EXPLOSION! You rolled..', number, '!')
number_exp = random.randint(0, sides)
if number_exp != sides:
rolls.append(number_exp)
#Once rolls have finished call heading function to display statistics.
print_heading('Statistics')
#Display statistics of rolls.
print('Total:', sum(rolls))
print('You rolled:', rolls)
#Calculate and print average of rolls. Convert value to float, round and store as variable average_roll_rounded.
average_roll = float(sum(rolls)/len(rolls))
average_roll_rounded = round(average_roll, 2)
print('Average:', average_roll_rounded)
#store max roll value to show occurence.
max_roll = int(max(rolls))
count = rolls.count(max_roll)
print('Maximum:',max_roll,'rolled',count,'time(s).')
#store min roll value to show occurence.
min_roll = int(min(rolls))
count = rolls.count(min_roll)
print('Minimum:',min_roll,'rolled',count,'time(s).')
else:
print('Invalid Entry. Please follow the format 4d6 or press "h" for help.')
and here is the requirement
Allow for rolls to be made with “exploding dice”. This means that if a die rolls the highest possible
number (i.e. sides), it is re-rolled and added to the result of that roll. This continues for as long
as the highest number is rolled, allowing a die to explode multiple times for a larger result.
To roll with exploding dice, the user enters their roll with a “!” at the end, e.g. “4d6!”.
Implementing this feature involves changes to several parts of the program:
5.1. Mention exploding dice in the information shown when the user enters “h”.
5.2. Alter your code for determining if a roll is valid to account for the possible presence of a “!”.
Consider checking whether the last character of the user’s input is “!” before splitting it. Set an
“exploding” variable to True or False as appropriate, then get rid of the “!” from the user’s input.
5.3. In the loop that rolls the dice, check whether the result of the current roll is equal to sides.
If so, print a message, roll the dice again, and keep a running total for the current roll.
As per the screenshot, the program should print each roll and the current total.
Use a loop to ensure that you keep re-rolling the dice as long as you keep rolling equal to sides.
It will take planning, experimentation, and refinement of your code to arrive at an elegant solution!
The “exploding dice” feature does not impact any other parts of the program. Dice with fewer sides explode
more often, so this feature is easier to test if you are rolling lots of few-sided dice, e.g. “10d4!”
if you had any idea on how i can get this to work that would be amazing thank you..