Two dice game in Python

Asked

Viewed 491 times

-2

I couldn’t do a function that simulates a two-dice game and counts how many times the dice were played until repeated numbers come out.

def dados():
dado1 = [random.randint(1,6)]
dado2 = [random.randint(1,6)]
if dado1 == dado2:
    return

My problem was not knowing what to return to count the number of times the daodos were played until repeated numbers came out.

  • 1

    If the answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

1 answer

4

Hello, first I’ll rewrite your function a little bit:

import random  # biblioteca com funções prontas para gerar valores aleatórios


def lancar_dados():
   cont = 0  # aqui vamos contar quantas vezes os dados foram lançados
   while True:
      dado1 = [random.randint(1,6)]
      dado2 = [random.randint(1,6)]
      cont += 1
      if dado1 == dado2:
          return cont

if __name__ == "__main__":
   print(lancar_dados())

The function will repeat until it finds the same values in dado1 and dado2. And returns the number of times the two dice have been cast.

  • 2

    you could add the import random, because some users can just copy and paste and give error, so it is easier for other people :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.