Question

In: Computer Science

import random # define functions def rollDice(): # function returns a random number between 1 and...

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()

Solutions

Expert Solution

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!


Related Solutions

A random number generator returns random floats between 0 and 1, according to a uniform distribution....
A random number generator returns random floats between 0 and 1, according to a uniform distribution. say you let the random number generator draw 100 numbers, the sample mean will be... A.impossible to determine, because the data aren't drawn from a normal distribution. B.normally distributed, with mean 0.5 and standard deviation 0.2887. C.uniformely distributed, between 0 and 1. D.normally distributed, with mean 0.5 and standard deviation 0.02887.
write pseudocode for this program . thank you import random class cal():    def __init__(self, a,...
write pseudocode for this program . thank you import random class cal():    def __init__(self, a, b):        self.a = a        self.b = b    def add(self):        return self.a + self.b    def mul(self):        return self.a * self.b    def div(self):        return self.a / self.b    def sub(self):        return self.a - self.b def playQuiz():    print("0. Exit")    print("1. Add")    print("2. Subtraction")    print("3. Multiplication")    print("4. Division")...
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive)....
C programming: Define a function computer_choose that chooses a random integer between 1 and 1000 (inclusive). Note: the tests call your function 1000 times to verify that no results are <0 or >1000. Rarely, this can score an incorrect function as correct.
import tkinter as tk import math window = tk.Tk() window.title("RSA Encryption/Decryption") window.geometry("500x500") #screen.configure(background="black") #functions def Modulus():...
import tkinter as tk import math window = tk.Tk() window.title("RSA Encryption/Decryption") window.geometry("500x500") #screen.configure(background="black") #functions def Modulus(): p = int(p_input1.get()) q = int(q_input1.get()) n = p*q    open_text = tk.Label(text="Keep your Messages Secure", fg = "white", bg = "blue") open_text.pack() Encryp_text = tk.Label(text="Encryption", fg="white", bg="red") Encryp_text.place(x=200, y=35) Decryp_text = tk.Label(text="Decryption", fg="white", bg="green") Decryp_text.place(x=200, y=270) p_input=tk.Label(text="Enter the value of P: ") p_input.place(x=10, y=58) p_input1=tk.Entry() p_input1.place(x = 130, y=58) q_input=tk.Label(text="Enter the value of q:") q_input.place(x=10, y=78) q_input1=tk.Entry() q_input1.place(x = 130, y=78) mod =...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Implement the following functions in the given code: def babylonian_square_root(N, estimate, precision): This function is provided...
Implement the following functions in the given code: def babylonian_square_root(N, estimate, precision): This function is provided for you to use: def close_enough(x, y, maximum_allowable_difference): My biggest piece of advice to you is to go one line at a time and check your return values after each little change you make. Starter code: # There are different ways of implementing the same idea in math. # Today we will explore a simple way to calculate square roots. # # # #...
package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number...
package week_2; import java.util.Random; import static input.InputUtils.*; /** * Your program should generate a random number between 0 and 9, and challenge the user to guess the number. Write a loop that asks the user to guess a number that the computer is thinking of. Print a success message if they guess correctly. If the user does not guess correctly, tell the user that they need to guess higher, or lower, and ask the user to try again. The user...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Define and Illustrate the Following: 1. A production function showing Increasing returns to Scale 2. A...
Define and Illustrate the Following: 1. A production function showing Increasing returns to Scale 2. A Long-Run Cost Function showing decreasing returns to Scale 3. Nash Equilibrium (Construct your Own example) 4. Cartel –like Oligopoly
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT