Dice Rolling Simulator using Python

 Dice Rolling Simulator using Python-random

Dice Rolling Simulator using Python


Python rolling a dice program will allow the user to roll a standard six-sided dice and display the result.

How The Program Performs: 

  • import random: It generates random numbers for dice rolls
  • def roll_dice(): A user-defined function generates a random integer number between 1 and 6.
  • main function: The main function prompts the user to roll the dice, press the enter key, and asks if they want to roll again. 
  • Infinite loop: The loop program asks the user to do continue or exit the program. 

Python Code


import random


def roll_dice():
    return random.randint(1, 6)


def main():
    print("Welcome to the Dice Roller!\n")
    while True:
        input("Press Enter to roll the dice...")
        result = roll_dice()
        print(f"You rolled a {result}!\n")

        play_again = input("Do you want to roll again? (yes/no): ").strip().lower()
        if play_again == 'yes':
            roll_dice()

        else:
            print("Thanks for playing! Goodbye!")
            break


if __name__ == "__main__":
    main()

Run the Program : 

  • Copy the code and paste your IDE(PyCharm or Notepad++ ). and name it as your choice.
  • Run the program .
  • and follow the on-screen instructions.
The Output : 

Code Output


Welcome to the Dice Roller!
Press Enter to roll the dice...
You rolled a 4!

Do you want to roll again? (yes/no): yes
Press Enter to roll the dice...
You rolled a 2!

Do you want to roll again? (yes/no): no
Thanks for playing! Goodbye!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Ok, Go it!