Dice Roller (code) in python

← Back to Projects

In this segment, there are different versions of this Dice Roller project. You can select,copy, paste and run either console or GUI code for dice roller.


Project Level: Beginner

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps:

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)


🎲 Python Dice Roller (Console)

This simple program simulates rolling a dice. It gives a number between 1 and 6 every time you press Enter:


import random

print("🎲 Welcome to the Dice Roller!")

while True:
    input("🎯 Press Enter to roll the dice...")
    dice = random.randint(1, 6)
    print(f"πŸ‘‰ You rolled a {dice}!")

    again = input("πŸ” Roll again? (y/n): ").strip().lower()
    if again != 'y':
        print("πŸ‘‹ Thanks for playing!")
        break


🎲 Python Dice Roller – GUI Version (Tkinter)

This beginner-friendly Python GUI app simulates rolling a dice using the tkinter module:


import tkinter as tk
import random

def roll_dice():
    result = random.randint(1, 6)
    label_result.config(text=f"🎲 You rolled: {result}")

# Create main window
root = tk.Tk()
root.title("🎲 Dice Roller")
root.geometry("300x200")
root.resizable(False, False)

# Create a label
label_title = tk.Label(root, text="🎲 Roll the Dice!", font=("Arial", 18))
label_title.pack(pady=10)

# Result label
label_result = tk.Label(root, text="Click the button to roll 🎯", font=("Arial", 16))
label_result.pack(pady=20)

# Roll button
roll_button = tk.Button(root, text="Roll Dice", font=("Arial", 14), bg="#4CAF50", fg="white", command=roll_dice)
roll_button.pack(pady=10)

# Start the GUI loop
root.mainloop()