Using Python:
Write a program which asks for a number of dice and the number of sides on the dice (you may assume all dice have the same number of sides). Your program should then roll the dice 100 times and print the average value rolled. For example:
If I choose one 6-sided die, the possible rolls are between 1 and 6. However, if I choose two 6-sided dice, the possible rolls are between 2 and 12. If I choose three 12-sided dice, the possible rolls are between 3 and 36.
For simplicity, you may assume the dice can have any positive number of sides greater than 1. If the user choose and invalid number of dice (less than 1) or an invalid number of sides (less than 2), your program should re-prompt the user for valid values.
import random n = int(input("Enter number of dice: ")) while(n<1): print("Invalid input.. Please try again") n = int(input("Enter number of dice: ")) sides = int(input("Enter number of sides: ")) while(sides<2): print("Invalid input.. Please try again") sides = int(input("Enter number of sides: ")) minValue = n maxValue = n*sides total = 0 for i in range(100): total += random.randint(minValue,maxValue) print("Average value rolled:",total//100)
Get Answers For Free
Most questions answered within 1 hours.