Python - rolling a dice fairly and counting how many 4's I get -
so had make code roll die , counted how many 4's got. of 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. should calculate both numbers in 1 run – counting 2 distinct things."
and code freshly fixed.
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 print ("in", n,"rolls of pair of dice, there were",twocount,"twos.") rollmanycounttwo(6000)
you shouldn't calling randrange() inside roll() in every if condition, instead should call once , save in variable , check.
the 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 rollmanycountfour(n): fourcount = 0 in range (n): if roll() == "6": fourcount += 1 print ("in", n,"rolls of due, there were",fourcount,"fours.") rollmanycountfour(6000)
Comments
Post a Comment