Waqar Rafique
Waqar Rafique

Reputation: 39

Want to get to know how many times double 6 appeared while rolling the dice to randomly generated number of time?

I just saved the results as string and now performing the check using the loop but its not working counter is still 0 if 66 appeared in the result.

from random import *

trial = int(randint(1, 10))
print(trial)
result = ''
for i in range(trial):
    init_num = str(randint(1, 6))
    result += init_num
print(result)

last_dice = 0
counter = 0
for i in range(trial):
    if result[i] == 6 and last_dice == 6:
        counter += 1
        last_dice = 0
    else:
        last_dice = result[i]

print(counter)

Upvotes: 1

Views: 141

Answers (1)

fuzzydrawrings
fuzzydrawrings

Reputation: 303

if result[i] == 6

This condition will never be true because the value in 'result' is a string. If you change the 6 to '6' it should work.

EDIT

"how many time double 6 appeared while rolling the dice" implies the dice are being rolled in pairs. As it is, your script will count any two consecutive sixes as having been rolled together. For example if the rolls are 4,6,6,5 your script counts this as an instance of double sixes, which is not accurate if the rolls are occurring in pairs. For this reason you should generate the random 1-6 values in pairs so it is clear when sixes are actually rolled together.

You could create 'results' as a list where each item is a pair of numbers representing a roll of two dice:

results = [str(randint(1,6))+str(randint(1,6)) for i in range(randint(1, 10))]

Your 'trials' variable is only used to print the number of rolls. If each roll is for a pair of dice, the number of rolls is equal to the number of items in the above 'results' list. Printing this value doesn't require a variable, but merely the length of the list:

print(len(results))

One way to print all the roll results is as an easily readable string of comma-separated roll pair values, which can be done printing this list joined together with each item separated by a comma:

print(','.join(results))

Counting the number of double sixes rolled can be done by summing the number of times the value '66' appears in the 'results' list:

print(sum(i == '66' for i in results))

All together the script could be written as:

from random import randint

results = [str(randint(1,6))+str(randint(1,6)) for i in range(randint(1, 10))]
print(len(results))
print(','.join(results))
print(sum(i == '66' for i in results))

Upvotes: 1

Related Questions