Crie um Simulador de Rolagem de Dados em Python
Najwa Riyaz
18 julho 2021
Python
Python Number

Para criar um simulador de lançamento de dados em Python, usamos a função random.randint()
que gera números aleatórios entre a faixa de números de 1 a 6 da seguinte forma.
random.randint(1, 6)
Crie um Simulador de Rolagem de Dados em Python usando random.randint (1,6)
Podemos criar um simulador de rolagem de dados em Python usando a função random.randint()
. A sintaxe da função é a seguinte.
random.randint(x, y)
Consequentemente, ele gera um número inteiro aleatório entre x
e y
. No exemplo do simulador de dados,
x
é 1 e y
é 6.
Abaixo está um exemplo.
import random
print("You rolled the following number", random.randint(1, 6))
Para permitir que o usuário escolha se deseja continuar jogando os dados ou não, podemos colocar random.randint(1,6)
dentro de um loop while
como segue.
from random import randint
repeat_rolling = True
while repeat_rolling:
print("You rolled the following number using the Dice -", randint(1, 6))
print("Do you wish to roll the dice again?")
repeat_rolling = ("y" or "yes") in input().lower()
Quando o usuário opta por parar de lançar os dados, ele deve sair do cicli while
.
Resultado:
You rolled the following number using the Dice - 2
Do you wish to roll the dice again?
y
You rolled the following number using the Dice - 4
Do you wish to roll the dice again?
y
You rolled the following number using the Dice - 5
Do you wish to roll the dice again?
n
Está gostando dos nossos tutoriais? Inscreva-se no DelftStack no YouTube para nos apoiar na criação de mais vídeos tutoriais de alta qualidade. Inscrever-se