Reputation: 5
Roll a pair of 6-sided dice (a.k.a. D6) until they both come up '1'. Count the number of rolls this took. Run 100 trials of this. Print out the result of each roll and report the average number of rolls required.
Use nested loops. The outer loop runs 100 trials; the inner loop continues rolling until 1-1 appears. Then update the running counts and go to the next trial.
import random
dice1, dice2 = " ", " "
roll = " "
for roll in range(1, 101):
roll = 0
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
print(dice1, ",", dice2)
while dice1 == 1 and dice2 == 1:
break
this doesn't stop when 2 1's a rolled and i need help accumulating the roll number and trial number
Upvotes: 1
Views: 287
Reputation: 304395
import random
from itertools import count
for roll in range(1, 101):
for c in count(1):
dice1 = random.randint(1, 6)
dice2 = random.randint(1, 6)
print(c, ':', dice1, ",", dice2)
if dice1 == 1 and dice2 == 1:
break
Upvotes: 0
Reputation: 77860
The problem is that your inner loop really doesn't do anything. You have to give it the work you described: keep rolling two dice until they both come up 1. I'll outline the logic you described, but have trouble implementing. I'll leave the detailed work to you. :-)
roll_count = 1
while not (dice1 == 1 and dice2 == 1):
roll both dice
increment roll_count
running_total += roll_count
You also need to initialize running_total somewhere.
Does this get you unstuck?
Upvotes: 1