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
- 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
- Print welcome message
print("Welcome to Dice Roller Test Game")
- Create a variable x and assign it value as "y".
x= "y"
- Now we will use while loop to print random dice every time.
while x == "y":
- Lets use the "random.randint()" to generate random numbers
number=random.randint(1,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("---------------")
- 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