Python - creating 2 dice to roll fairly and add them together? -
so had make code roll die , counted how many 4's got. of people on here got work. have created die , roll them , add products together. instructions i've been given.
"then write function simulates rolling 2 fair dice. easy way call function wrote, twice, , add numbers get. should return number between 2 , 12."
i've added rolling dice second time how add sums of 2 rolls question? , 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 in range (n): if roll() == "2": twocount += 1 if roll() == "2": twocount +=1 print ("in", n,"rolls of pair of dice, there were",twocount,"twos.") rollmanycounttwo(6000)
you shouldn't have deal strings @ all, done entirely using int
values
from random import randint def roll(): return randint(1,6) def roll_twice(): total = 0 turn in range(2): total += roll() return total
for example
>>> roll_twice() 10 >>> roll_twice() 7 >>> roll_twice() 8
and function supposed count number of 2
s rolled, again can integer comparison
def rollmanycounttwo(n): twos = 0 turn in range(n): if roll() == 2: twos += 1 print('in {} rolls of pair of dice there {} twos'.format(n, twos)) return twos >>> rollmanycounttwo(600) in 600 rolls of pair of dice there 85 twos 85
Comments
Post a Comment