In: Computer Science
Write the functions necessary to simulate a gambling game with the following rules:
The winner is the player who has a total closest to 9 without going over.
1. The player "rolls" 2 dice (6 sided dice)
2. The computer "rolls" 2 dice (6 sided dice)
3. The user is asked whether they want to roll one more time. If they indicate they do, another dice is rolled and added to the player's total.
4. A message is printed indicating the winner. In the case of a tie, the player wins. If both the computer and player go over 9, the computer wins.
5. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER (1 or 2) AS THEIR CHOICE.
You may NOT change the main function except to insert the if statement and print statements as indicated.
# 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()
Here is the solution,
# your code starts here
"""
Rules of the Game (as per the question)
1) Both player(human) and Computer roll the dice twice.
2) Player(human) may roll the dice one more time
3) take total of computer roll dice values
4) take total of player(human) roll dice values
5) who evers total is closet to 9, wins
6) who ever goes over 9, will lose.
7) if it is a tie, then player wins
8) if both the player(human) and the computer goes over 9, computer
wins
"""
import random
def rollDice():
return random.randint(1,6)
def userWon(player,computer):
# if it is a tie...player(human) wins
if player == computer:
print("Both player and computer have same values...")
return True
# if both the computer and the user goes over 9, computer
wins
elif player > 9 and computer > 9:
print("Both player and computer goes over 9, Computer:",computer,"
, Player:", player)
return False
# if player goes over 9 and computer is below 9, then computer
wins
elif player > 9 and computer < 9:
print("Computer :",computer,","," Player :",player)
return False
# if computer goes over 9 and player is below 9, then player
wins
elif player < 9 and computer > 9:
print("Computer :",computer,","," Player :",player)
return True
# if player and computer are below 9, then check for closet to
9
elif player < 9 and computer < 9:
# if player's total is closet compared to computer then player
wins
if(9-player) < (9-computer):
print("Computer :",computer,","," Player :",player)
return True
# else computer wins
else:
print("Computer :",computer,","," Player :",player)
return False
def main():
print()
print()
#each player rolls two Dice
player = rollDice() + rollDice()
computer = rollDice() + rollDice()
print("Player(human) and computer have rolled the dice
twice.")
# ask the player if they want to roll again
again = int(input("Please enter 1 to roll again. Enter 2 to hold:
"))
if again == 1:
player = player + rollDice()
# check who has won
if userWon(player, computer):
print("player(human) Won")
else:
print("Computer Won")
# function call
main()
# code ends here
here is the sample output:-
here is the screenshot of the
code for better understanding of indentation:-
Thank You.