d2jsp
d2jsp Forums > Off-Topic > Programmer's Haven > Web Languages > Need Python Help
Add Reply New Topic New Poll
reading123
Group: Member
Posts: 11,588
Joined: Sep 6 2007
Gold: 14,367.00
Mar 27 2022 07:08am
will donate FG to person who can resolve my query(s)

thank you
codeMonk
Group: Member
Posts: 5,892
Joined: Feb 7 2009
Gold: 2,943.37
Mar 28 2022 06:42am
Pm me :-)
reading123
Group: Member
Posts: 11,588
Joined: Sep 6 2007
Gold: 14,367.00
Mar 29 2022 08:35pm
Quote (codeMonk @ Mar 28 2022 08:42pm)
Pm me :-)


pm'ed!!

if anyone else can help that would be amazing thank you
Candyzcanes
Group: Member
Posts: 21,755
Joined: Dec 6 2008
Gold: 1,150.69
Trader: Mediator
Mar 29 2022 08:49pm
Quote (reading123 @ Mar 29 2022 08:35pm)
pm'ed!!

if anyone else can help that would be amazing thank you


get more help if you posted what you needed the help with ;)
reading123
Group: Member
Posts: 11,588
Joined: Sep 6 2007
Gold: 14,367.00
Mar 29 2022 10:23pm
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..
CocaineCowboyz
Group: Member
Posts: 4,203
Joined: Jan 29 2009
Gold: 11,291.60
Mar 30 2022 07:15pm
hi this should work, a dice cannot contain 0 sides so the random must start at 1, as for the explosion you juste need to loop through the rolls, i added a few comments. The indentation was the challenge here haha

Code
# Import random module
import random


# Function for heading.
def print_heading(text, width=50):
print('=' * width)
print('|' + text.center(48) + '|')
print('=' * width)


def main():
# 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')
exit(0)

# Reassign roll_text to return true if ! is present at the end of initial use input.
explode = (roll_text.endswith('!'))
if explode:
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(1, sides)
print('You rolled..', number, '!')
rolls.append(number)

# explode is true and a max roll is obtained, perform explode function.
if explode:
#loop through the rolls
for r in rolls:
# if the roll is mathching the number of sides reroll the dice and add it to the rolls list
if r == sides:
number_exp = random.randint(1, sides)
print('EXPLOSION! You rolled..', number_exp, '!')
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.')


if __name__ == "__main__":
main()


This post was edited by CocaineCowboyz on Mar 30 2022 07:16pm
reading123
Group: Member
Posts: 11,588
Joined: Sep 6 2007
Gold: 14,367.00
Mar 30 2022 07:57pm
Quote (CocaineCowboyz @ Mar 31 2022 09:15am)
hi this should work, a dice cannot contain 0 sides so the random must start at 1, as for the explosion you juste need to loop through the rolls, i added a few comments. The indentation was the challenge here haha

Code
# Import random module
import random


# Function for heading.
def print_heading(text, width=50):
print('=' * width)
print('|' + text.center(48) + '|')
print('=' * width)


def main():
# 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')
exit(0)

# Reassign roll_text to return true if ! is present at the end of initial use input.
explode = (roll_text.endswith('!'))
if explode:
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(1, sides)
print('You rolled..', number, '!')
rolls.append(number)

# explode is true and a max roll is obtained, perform explode function.
if explode:
#loop through the rolls
for r in rolls:
# if the roll is mathching the number of sides reroll the dice and add it to the rolls list
if r == sides:
number_exp = random.randint(1, sides)
print('EXPLOSION! You rolled..', number_exp, '!')
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.')


if __name__ == "__main__":
main()


pmed
aanfazi54
Group: Member
Posts: 18
Joined: Jun 7 2022
Gold: 0.00
Jun 22 2022 10:14pm
Hi, did it finished/solved the problem yet?

This post was edited by aanfazi54 on Jun 22 2022 10:17pm
Go Back To Web Languages Topic List
Add Reply New Topic New Poll