Senor Penguin - 1 year ago 50

Python Question

Just going through some practice exercises, this one asks to create a dice roller that will generate a random number. The arguments of the function are supposed to be (number of sides on dice, Number of Rolls)

If I just use return it will stop the loop. I know a way around this is to combine the answers and print that but what if the amount of answers (or rolls in this case) is a variable?

Here is my code:

`#roll_dice`

import random

def rolldice(sides, rolls):

start = 1

while start <= rolls:

number = random.randint(1, sides)

start += 1

return number

print rolldice(6, 3)

How can I get this to print this:

`3`

5

2

Answer Source

you can use `yield`

```
import random
def rolldice(sides, rolls):
for i in xrange(0, rolls):
yield random.randint(1, sides)
for item in rolldice(6,3):
print item
```