Python Roll dice with 2 parameters: Number of sides of the dice and the number of dice

Question:

  1. This is the problem I have: Write a function roll dice that takes in 2 parameters – the number of sides of the die, and the number of dice
    to roll – and generates random roll values for each die rolled. Print out each roll and then return the string
    “That’s all!” An example output

       >>>roll_dice(6,3)
       4
       1
       6 
       That's all!
    
  2. This is the code I have so far using a normal roll dice code:

                      import random
    
                      min = 1
                      max = 6
    
                      roll_dice = "yes"
                      while roll_dice == "yes":
                          print random.randint(min,max)
                          print random.randint(min,max)
                          print "That's all"
    
                          import sys
                          sys.exit(0)
    
Asked By: Anacletoxx

||

Answers:

Try this:

def roll_dice(sides, rolls):
    for _ in range(rolls):
        print random.randint(1, sides)
    print 'Thats all'

This uses a for loop to loop rolls amount of times and prints a random number between 1 and sides each loop.

Answered By: gommb
import random
def roll_dice(attempts,number_of_sides):
  for i in range(attempts):
     print(random.randint(1, number_of_sides))
  print("thats all")
Answered By: kar

In a more advanced, you can have two parameters where you ask the users input to set the values and it will run the functions and give the numbers rolled, the sum and ask if the user wants to try again.

#Import ‘random’ module
import random

def main():

#Get values from user to determine slides_per_dice and number_of_die.
sides_per_die = get_value(1,50, 'How many sides should the dice to have 
(1-50): ', 'That is not a correct response, enter a value between 1 and 
50')

number_of_dice = get_value(1,10, 'How many dice should be rolled (1-10): 
', 'That is not a correct response, enter a value between 1 and 10')

#Simulate rolling specified number of dice with specified sides on each 
dice.

outcome = rolling_dice (sides_per_die, number_of_dice)

#Asking user if they would like to roll again.
roll_again = input("Would you like to play again (y/n): ") .lower()
while roll_again != 'n':
    if roll_again =='y':
        main()
    else:
        print('Error: please enter "y" or "n".')
    roll_again = input("Would you like to play again (y/n): ")

#Final message if user ends the game.
print('Thanks for playing.')

#Asking user for side_per_dice and number_of_nice input.

def get_value(lower_limit, upper_limit, prompt, error_message):
number = int(input(prompt))
while number < lower_limit or number > upper_limit:
print(error_message)
number = int(input(prompt))
return number

#Determining the outcome of the rolls and summing up total for all rolls.

def rolling_dice (sides_per_die, number_of_die):
roll_sum = 0
print(‘Rolling dice …..’)
for roll in range (number_of_die):
result = random.randint(1, sides_per_die)
roll_sum += result
print(result, end = " ")
print(f’nThe sum of all dice rolled is {roll_sum}’)

main()

Answered By: Justin Grima
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.