Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The counter still appears "0" despite the appearance of double 66 in the result.
After that I want to check the result for longest sequence of successive rolls without six. but don't know how to get started. help please.

What I have tried:

<pre>from random import *

trial = int(randint(1, 70))
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)
Posted
Updated 30-Apr-21 9:00am
Comments
Richard MacCutchan 1-May-21 4:10am    
I gave you a simple solution to this problem yesterday.

Quote:
The counter still appears "0" despite the appearance of double 66 in the result.

The trick is that new you made result a string, and that imply that you need to compare with strings.
Python
<pre>from random import *

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

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

print(counter)
 
Share this answer
 
result is a total, not an array of values: if it was an array, then your total display wouldn't work:
Python
for i in range(trial):
    init_num = str(randint(1, 6))
    result += init_num
print(result)
So, you can't assume that you will have the previous set of values stored in it!

Think about it: why do you have two loops at all? Could you do it with no array and just one loop?
Hint: "yes" :D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900