Dheeraj Choudhary
Dheeraj Choudhary's Blog

Dheeraj Choudhary's Blog

Python Dice Simulator Text Game

Dheeraj Choudhary
·Sep 3, 2021·

2 min read

Python Dice Simulator Text Game

Subscribe to my newsletter and never miss my upcoming articles

This project involves writing a program that simulates rolling dice. When the program runs, it will randomly choose a number from 1 and 6 using randint() method. The program will print our text dice output based on the random number generated. It should then ask you if you’d like to roll again or exit.

Dice Simulator Program

  1. Lets import random module Python Random Module is a built-in module that you can use to make random numbers.
    Python uses a popular and robust pseudorandom number generator called the Mersenne Twister. Check out official documentation here for indepth understanding.
    import random
    
  2. Print welcome message
    print("Welcome to Dice Roller Test Game")
    
  3. Create a variable x and assign it value as "y".
    x= "y"
    
  4. Now we will use while loop to print random dice every time.
    while x == "y":
    
  5. Lets use the "random.randint()" to generate random numbers
    number=random.randint(1,6)
    
  6. Based on the random numbers generated below output will be printed.

     if number == 1:
         print("---------------")
         print("|             |")
         print("|      0      |")
         print("|             |")
         print("---------------")
    
     if number == 2:
         print("---------------")
         print("|             |")
         print("| 0         0 |")
         print("|             |")
         print("---------------")
    
     if number == 3:
         print("---------------")
         print("|      0      |")
         print("|      0      |")
         print("|      0      |")
         print("---------------")
    
     if number == 4:
         print("---------------")
         print("| 0         0 |")
         print("|             |")
         print("| 0         0 |")
         print("---------------")
    
     if number == 5:
         print("---------------")
         print("| 0         0 |")
         print("|      0      |")
         print("| 0         0 |")
         print("---------------")
    
     if number == 6:
         print("---------------")
         print("| 0    0    0 |")
         print("|             |")
         print("| 0    0    0 |")
         print("---------------")
    
  7. After providing the output user input is required to continue or quit as below
    x= input(" Would you like to continue?... \n Press y to continue and n to exit the game: ")
    

Find the code at Github location here

So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.

Did you find this article valuable?

Support Dheeraj Choudhary by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this