Answers for "rolling dice game python code"

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()
Posted by: Guest on July-11-2020
0

rolling dice game python code

# Needed to create random numbers to simulate dice roll
import random

# Initialise player scores to 0
player1_score = 0
player2_score = 0

# Repeat everything in this block 10 times
for i in range(10):

    # Generate random numbers between 1 and 6 for each player.
    player1_value = random.randint(1, 6)
    player2_value = random.randint(1, 6)

    # Display the values
    print("Player 1 rolled: ", player1_value)
    print("Player 2 rolled: ", player2_value)

    # Selection: based on comparison of the values, take the appropriate path through the code.
    if player1_value > player2_value:
        print("player 1 wins.")
        player1_score = player1_score + 1  # This is how we increment a variable
    elif player2_value > player1_value:
        print("player 2 wins")
        player2_score = player2_score + 1
    else:
        print("It's a draw")

    input("Press enter to continue.")  # Wait for user input to proceed.

print("### Game Over ###")
print("Player 1 score:", player1_score)
print("Player 2 score:", player2_score)
Posted by: Guest on October-25-2021

Python Answers by Framework

Browse Popular Code Answers by Language