In our previous Python tutorial, we have explained to make a Password Cracker using Python. In this tutorial, we will create Dice Rolling Simulator in Python.
Python is a popular language among beginners beacause it’s easy to learn and can make amzing things. Here in this tutorial, we will use random
module and create a classic rolling dice simulator using Python.
So let’s code to implement it,
Create Dice Rolling Simulator
We will include random
module to create dice rolling simulator by taking random number.
import random
We will take user input to roll again or exit from program.
x = input("\nPress y to roll again and n to exit:") print("\n")
We will initialize variable x
with value y
to start create dice and then use while
loop to check for user value y
to create dice by checking random number from 1 to 6.
x = "y" while x == "y": randNumber = random.randint(1,6) if randNumber == 1: print("\n[-----]") print("[ ]") print("[ 0 ]") print("[ ]") print("[-----]") if randNumber == 2: print("\n[-----]") print("[ 0 ]") print("[ ]") print("[ 0 ]") print("[-----]") if randNumber == 3: print("\n[-----]") print("[ ]") print("[0 0 0]") print("[ ]") print("[-----]") if randNumber == 4: print("\n[-----]") print("[0 0]") print("[ ]") print("[0 0]") print("[-----]") if randNumber == 5: print("\n[-----]") print("[0 0]") print("[ 0 ]") print("[0 0]") print("[-----]") if randNumber == 6: print("\n[-----]") print("[0 0 0]") print("[ ]") print("[0 0 0]") print("[-----]")
Complete Code
Here is the complete code to create Dice Rolling Simulator in Python.
import random x = "y" while x == "y": randNumber = random.randint(1,6) if randNumber == 1: print("\n[-----]") print("[ ]") print("[ 0 ]") print("[ ]") print("[-----]") if randNumber == 2: print("\n[-----]") print("[ 0 ]") print("[ ]") print("[ 0 ]") print("[-----]") if randNumber == 3: print("\n[-----]") print("[ ]") print("[0 0 0]") print("[ ]") print("[-----]") if randNumber == 4: print("\n[-----]") print("[0 0]") print("[ ]") print("[0 0]") print("[-----]") if randNumber == 5: print("\n[-----]") print("[0 0]") print("[ 0 ]") print("[0 0]") print("[-----]") if randNumber == 6: print("\n[-----]") print("[0 0 0]") print("[ ]") print("[0 0 0]") print("[-----]") x = input("\nPress y to roll again and n to exit:") print("\n")
Output:
[-----] [0 0 0] [ ] [0 0 0] [-----] Press y to roll again and n to exit:y [-----] [ 0 ] [ ] [ 0 ] [-----] Press y to roll again and n to exit:y [-----] [0 0] [ 0 ] [0 0] [-----]