Python Dice Simulator Text Game

Python Dice Simulator Text Game

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!