jeudi 22 mars 2018

Python: Building a dice rolling program that rolls until all dice land on 6

I want to preface by saying that as a beginner in python, I really enjoyed giving myself a small project and trying things out till it finally worked. I understand there's no one single way of solving a problem and my way is very primitive (even though it does get the job done), I would like to know how an experienced programmer would solve it.

My goal was: write a script to roll 3 die, until all 3 land on the number 6, as well as count the number of rolls it took for the goal to complete.

Here's my attempt:

import random
count = 0

while True:
    roll = random.randint(1,6)
    roll2=random.randint(1,6)
    roll3=random.randint(1,6)
    print(roll,roll2,roll3)
    if roll !=6 or roll2!=6 or roll3!= 6:
        count +=1
        continue
    if roll ==6 and roll2==6 and roll3==6:
        count +=1
        print('u did it. Number of tries: ' + str(count))
        break




Aucun commentaire:

Enregistrer un commentaire