-3

Write a function DiceRoll(n) that inputs an integer n and produces n random numbers between 1 and 6. Test your program for n = 12.

I got this for that :

import random

def DiceRoll(n):
    x=[random.randint(1,6) for _ in range(n)]
    return x

Then,

Write a function TwoDiceRoll(n) that uses your function DiceRoll to sum the result of rolling two random dice n times. Test your program for n = 12.

I do not have an idea how would I involve my DiceRoll function in order to get the sum. Would someone be able to help me out.

TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19
ADAM
  • 105
  • 1
  • 7

2 Answers2

-1

Code:

import random

def TwoDiceRoll(n):
    d1=DiceRoll(n)
    d2=DiceRoll(n)
    dsum=[i+j for i,j in zip(d1, d2)]

    return d1,d2,dsum

def DiceRoll(n):
    x=[random.randint(1,6) for _ in range(n)]
    return x

x=DiceRoll(12)
print x

d1,d2,dsum=TwoDiceRoll(12)
print d1, "\n", d2, "\n", dsum

Example Output:

# It will be different everytime because of random function.
[3, 2, 3, 6, 4, 3, 5, 4, 4, 4, 2, 4]
[1, 4, 1, 2, 4, 1, 6, 5, 2, 6, 6, 5]
[4, 2, 3, 1, 6, 3, 1, 5, 5, 2, 6, 3]
[5, 6, 4, 3, 10, 4, 7, 10, 7, 8, 12, 8]
RAVI
  • 3,143
  • 4
  • 25
  • 38
  • 1
    You didn't understand the question and yet answered anyway, there's zero explanation, and it's wrong (same as the other answer). – TigerhawkT3 Sep 05 '16 at 00:36
  • Tell us what we understood wrong? As per question you mentioned about writing TwoDiceRoll() method and getting sum and using DiceRoll() method as well. – RAVI Sep 05 '16 at 00:38
  • Think about it: what's the last time you wanted to roll a pair of dice several times and just add everything together into a single number? – TigerhawkT3 Sep 05 '16 at 00:39
  • 1
    We understood the question fine, the reasoning behind it was a little blurred. The OP asked for the sum of the results, is it our job to attempt to assume that he worded his question incorrect and interpret it in the sanest way possible? I don't think it is, he asked, and he received... – TheLazyScripter Sep 05 '16 at 00:45
  • I think you wanted to get pair wise sum. Check updated answer. And next time onwards explain you question properly, may be with at least one example. – RAVI Sep 05 '16 at 00:49
  • """Think about it: what's the last time you wanted to roll a pair of dice several times and just add everything together into a single number?""" Instead of giving reason like this you should give proper explanations of question, as there can be many reasons like, for adding everything together into a single number to find average outcome(sum/n). – RAVI Sep 05 '16 at 01:02
-2

Unsure of why you want to sum them but here you go! I am sure you are capable of wrapping this in a function!

import random

def DiceRoll(n):
    x=[random.randint(1,6) for _ in range(n)]
    return x

d1 = DiceRoll(12)
d2 = DiceRoll(12)
print sum(d1+d2)
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19