Dice Rolling Simulator using Python-random
In this article, we will create a classic rolling dice simulator with the help of basic Python knowledge. Here we will be using the random module since we randomize the dice simulator for random outputs.
Function used:
1) random.randint(): This function generates a random number in the given range. Below is the implementation.
Example 1: Dice Simulator
Python3
import random
x = "y"
while x = = "y":
no = random.randint( 1 , 6 )
if no = = 1 :
print ("[ - - - - - ]")
print ("[ ]")
print ("[ 0 ]")
print ("[ ]")
print ("[ - - - - - ]")
if no = = 2 :
print ("[ - - - - - ]")
print ("[ 0 ]")
print ("[ ]")
print ("[ 0 ]")
print ("[ - - - - - ]")
if no = = 3 :
print ("[ - - - - - ]")
print ("[ ]")
print ("[ 0 0 0 ]")
print ("[ ]")
print ("[ - - - - - ]")
if no = = 4 :
print ("[ - - - - - ]")
print ("[ 0 0 ]")
print ("[ ]")
print ("[ 0 0 ]")
print ("[ - - - - - ]")
if no = = 5 :
print ("[ - - - - - ]")
print ("[ 0 0 ]")
print ("[ 0 ]")
print ("[ 0 0 ]")
print ("[ - - - - - ]")
if no = = 6 :
print ("[ - - - - - ]")
print ("[ 0 0 0 ]")
print ("[ ]")
print ("[ 0 0 0 ]")
print ("[ - - - - - ]")
x = input ("press y to roll again and n to exit:")
print ("\n")
|
Output:
Example 2: Dice simulator
Python
import random
while True :
print ( )
user = int ( input ( "what you want to do\n" ))
if user = = 1 :
number = random.randint( 1 , 6 )
print (number)
else :
break
|
Output:
Last Updated :
13 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...