Beginner question python random numbers

Hi, beginner python question.

The task is, throw 3 dices and see how many throws it takes before all three dices show the same value. The number of repitions should print with the numbers generated.

I dont understand how I should write the while loop code and make it stop when I have the same value on the dices.

This is what I got so far, I dont understand where to go from here.

import random 

def terninger(start, end): 
    result = []
    for i in range(1):
        tall = random.randint(1,6)
    return result

a = terninger(1,6)
b = terninger(1,6)
c = terninger(1,6)


Welcome, F_S.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

The while-loop has a condition and as long as that condition is true, the loop keeps running.
The loop should stop if all 3 values are the same, so it should run while they are different aka a!=b or b!=c or a!=c

Now if you want to know how many throws it took, you just write a counter that increases in every loop.

By the way, what’s the point of that function? All it does is return random.randint(1,6).
You could just call a=random.randint(1,6) for the exact same result.

import random

a = random.randint(1,6)
b = random.randint(1,6)
c = random.randint(1,6)

n = 0
ferdig = False

while not ferdig: 
    if a!=b or b!=c or c!=a:
        a = random.randint(1,6)
        b = random.randint(1,6)
        c = random.randint(1,6)
        n += 1
        print('%3d. Kast: %d %d %d' % (n, a,b,c))
        ferdig = a==b and b==c and c==a
        if ferdig:
            print('Etter %d kast med tre terninger viste alle terningene verdien %d.' % (n, a))

Thanks for the help, this is the code I got.

Looks good ^^
You can save a little work by utilizing the while-loop more.
As it will test the condition itself and stops once it’s false, you don’t need to create “ferdig” seperatly.
Something like a,b,c = 1,2,3 will make sure you enter the loop and they are randomized until they are the same.
Similarly, once they are the same, you will leave the loop and could put the final print statement there instead of another if-clause.

Thank you for good feedback :grinning: