In: Computer Science
import random
# define functions
def rollDice():
# function returns a random number between 1 and 6
def userWon(t1, t2):
# function accepts player total and computer total
# function returns true if player wins
# function returns false if computer wins
def main():
# each player rolls two Dice
player = rollDice() + rollDice()
computer = rollDice() + rollDice()
# ask the player if they want to roll again
again = int(input("Please enter 1 to roll again. Enter 2 to
hold."))
# roll again if needed and add to player's total
if again == 1:
player = player + rollDice()
# insert your if statement here to determine the winner
# and your appropriate print statements
main()
Have a look at the below code. I have put comments wherever requr=ired for better understanding.
import random # import random library
# create function to generate random number
def rollDice():
return random.randint(1,6)
# function to check for the winner
def userWon(t1,t2):
return t1>t2
# main function
def main():
# player turn
player = rollDice() + rollDice()
# computer turn
computer = rollDice() + rollDice()
# check if player want to roll the dice again
again = int(input("Please enter 1 to roll again. Enter 2 to hold. \n"))
if (again==1):
player = player + rollDice()
# check for the winner using userWon function
if (userWon(player,computer)):
print("Player Wins!!!")
else:
print("Computer Wins!!!")
main() # call the main function
Happy Learning!