dice game python code example

Example 1: dice rolling simulator python

from random import randint

def roll_dice():
    print(f"Number is: {randint(1,6)}")

# Do this to simulate once
roll_dice()   

# Do this to simulate multiple times
whatever = 12 # Put the number of times you want to simulate here
for number in range(whatever):
    roll_dice()

Example 2: how to make a dice in python

#For one time roll
from random import randint

dice_roll = randint(1,6) 
#You can change numbers to anything you want, it can go up to 1 million if you really want it to
print("You Threw a", dice_roll)
#That's for one time throw but if you want it for multiple people then do this

#For multiple roles 
import time
from random import randint
for j in range(10):
    dice_roll = randint(1,6)
    time.sleep(3)
    print("You threw a", dice_roll)

Example 3: 2 plater die game in python

import random

def get_name(key):
    n = ""
    while not n:
        n = input(f"Input your name, {key}: ")
    return n

def throw_dice(key):
    dices = random.choices(range(1,7),k=2)
    print(f"{names[key]} threw: {dices}", end = " ")
    if sum(dices) % 2 == 0:
        score[key] += 10
        print("Even. You earn 10 points.")
    else:
        score[key] -= 5
        print("Odd. You loose 5 points")

def print_score():
    for n in score:
        print(f"  {names[n]} has got {score[n]} points.")

def win_message(s,n):
    print_score()
    if score["P1"] > score["P2"]:
        print(f"{names['P1']} won")
    else:
        print(f"{names['P2']} won") 



player = ""
score = {}
names = {}

for n in ["P1","P2"]:
    names[n] = get_name(n)
    score[n] = 0

# twice the amount of rows wanted, because players take turn

for c in range(10):
    # switches between player1 and player2
    player = "P1" if player != "P1" else "P2"

    print(f"Round {c//2 + 1}: it is {names[player]}'s turn.")
    print_score()
    throw_dice(player)

win_message(score,names)