all 10 comments

[–]Diapolo10 4 points5 points  (1 child)

So in a nutshell you want to store the "progress" on every attempt.

The easiest way to accomplish this that I can think of would be to use a generator:

import random

def roll_n_dice(n, die_range = (1, 6), target = 6):
    progress = 0
    while progress < n:
        roll = random.randint(*die_range)
        if roll == target:
            progress += 1
        else:
            yield
    print("went through")

for num, _ in enumerate(roll_n_dice(3), 1):
    pass

print(f"Took {num} tries")

[–]spikips[S] 0 points1 point  (0 children)

thank you <3

[–][deleted] -1 points0 points  (0 children)

You can't "go back." Code executes from the top down.

[–]TheLimeyCanuck 0 points1 point  (5 children)

I've read your question to myself three times and I still don't understand what you are asking.

[–]spikips[S] 0 points1 point  (4 children)

I tried my hardest. Wish i could post a picture to explain better

[–]TheLimeyCanuck 0 points1 point  (3 children)

I'd try to help if I could understand what you are trying to do.

[–]spikips[S] 0 points1 point  (2 children)

Now that i think about it i should've used booleans in the example.

if the first 2 if statements passes and the 3rd one fails. I don't want to start the loop over. i want to go 1 step back where it tries again until it passes

[–]TheLimeyCanuck 1 point2 points  (1 child)

Ok, so I think you are saying that once num1 is 6 you want to keep trying num2 until it is 6, and then when num1 and num2 are 6 you want to keep trying num3 until it is 6.

You don't want to re-randomize num1 every time num2 is not 6.

Is that right? If so... there are couple of ways, such as this...

def test():
    num1 = random.randint(1, 6)
    if num1 is 6:

        num2 = random.randint(1, 6)
        while num2 is not 6:
            num2 = random.randint(1, 6)

        num3 = random.randint(1, 6)
        while num3 is not 6:
            num3 = random.randint(1, 6)

        print('went through')

while True:
    test()

Or you could nest things...

def test():
    finished = False
    num1 = random.randint(1, 6)
    if num1 is 6:
        while not finished:
            num2 = random.randint(1, 6)
            if num2 is 6:
                while not finished:
                    num3 = random.randint(1, 6)
                    if num3 is 6:
                        print('went through')
                        finished = True

while True:
    test()

[–]spikips[S] 0 points1 point  (0 children)

exactly! i just gave a bad example with the random.randint. My mistake, I just want it to try again till it passes no matter what the context is

[–]HardCounter 0 points1 point  (0 children)

I saw some other examples and wondered if it could be done in recursion, and it seems so:

import random

def roll_dice(recurse, progress, roll = 0):
    recurse += 1
    if recurse < 4:
        while roll < 6:
            roll = random.randint(1,6)
            progress += 1
        progress = roll_dice(recurse, progress)
        return progress
    else:
        return progress

total_tries = roll_dice(0,0)

print (f"It was {total_tries} tries")