Python dice roll function – Python Program for Random Dice Roll

Python dice roll function: Small projects, such as a dice-rolling application with a text-based user interface (TUI), will help you improve your Python programming skills.

The program is commonly referred to as “Roll the dice.”

We will use the random module and the inbuilt randint() function to do this. randint() is primarily concerned with generating random numbers based on the minimum and maximum values specified in the arguments.

Syntax:

random.randint(min, max)

Integer values should be used for Min and Max. A random number will be created at random from the two specified numbers. Here are the minimum and maximum values.

We set the min to “1” and the max to “6” since the minimum number on the dice is 1 and the maximum number on the dice is 6. The dice numbers are 1, 2, 3, 4, 5, 6. As a result, we set the minimum and maximum values to 1 and 6, respectively.

Program for Random Dice Roll in Python

Approach:

  • Import random module using the import keyword.
  • Take a variable and initialize the minimum value with 1
  • Take another variable and initialize the maximum value with 6
  • Set the flag value to “yes” by default
  • Loop until the flag value == “yes” or “y” using the while loop
  • Printing some random text
  • Pass the above minimum and maximum values as the arguments to the randint() function of the random module to generate some random number and print the result
  • Give the string as user input using the input() function and store it in a variable.
  • The Exit of the Program.

Below is the implementation:

# Import random module using the import keyword.
import random
# Take a variable and initialize the minimum value with 1
min_value= 1
# Take another variable and initialize the maximum value with 6
max_value = 6
# Set the flag value to "yes" by default
flag = "yes"
# Loop until the flag value == "yes" or "y" using the while loop
while flag == "yes" or flag == "y":
    # Printing some random text
    print("The Dice is rolling")
    print ("The rolled random number on the dice:")
    # Pass the above minimum and maximum values as the arguments to the 
    # randint() function of the random module to generate some random number.
    # and print the result
    print(random.randint(min_value, max_value))
    # Give the string as user input using the input() function and store it in a variable.
    flag = input("Do you want to Roll the dice again?")
    print()

Output:

The Dice is rolling
The rolled random number on the dice:
3
Do you want to Roll the dice again?y

The Dice is rolling
The rolled random number on the dice:
5
Do you want to Roll the dice again?y

The Dice is rolling
The rolled random number on the dice:
1
Do you want to Roll the dice again?n