So I had to make code that roll a die fairly and counted how many 4's I got. With the help of people on here I got it to work. Well now I have to created another die and roll them and then add they products together. This is the instructions I've been given.
"Then write another function that simulates rolling two fair dice. The easy way is to call the function you just wrote, twice, and add the numbers you get. This should return a number between 2 and 12."
I've added rolling the dice a second time but how do I add the sums of the two rolls together is my question? And this is my code.
from random import randrange
def roll():
rolled = randrange(1,7)
if rolled == 1:
return "1"
if rolled == 2:
return "2"
if rolled == 3:
return "3"
if rolled == 4:
return "4"
if rolled == 5:
return "5"
if rolled == 6:
return "6"
def rollManyCountTwo(n):
twoCount = 0
for i in range (n):
if roll() == "2":
twoCount += 1
if roll() == "2":
twoCount +=1
print ("In", n,"rolls of a pair of dice, there were",twoCount,"twos.")
rollManyCountTwo(6000)
You shouldn't have to deal with strings at all, this could be done entirely using
int
valuesFor example
And for your function that is supposed to count the number of
2
s that were rolled, again you can do integer comparison