pythonrandomdice

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


  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)
    

Solution

  • Try this:

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

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