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

import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(")...
import random #the menu function def menu(list, question): for entry in list: print(1 + list.index(entry),end="") print(") " + entry) return int(input(question)) plz explain this code
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.
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2...
import random import turtle def isInScreen(win,turt): leftBound = -win.window_width() / 2 rightBound = win.window_width() / 2 topBound = win.window_height() / 2 bottomBound = -win.window_height() / 2 turtleX = turt.xcor() turtleY = turt.ycor() stillIn = True if turtleX > rightBound or turtleX < leftBound: stillIn = False if turtleY > topBound or turtleY < bottomBound: stillIn = False return stillIn def main(): wn = turtle.Screen() # Define your turtles here june = turtle.Turtle() june.shape('turtle') while isInScreen(wn,june): coin = random.randrange(0, 2) if...
Exercise Define a function that generates a random code. The functions takes in a string colors...
Exercise Define a function that generates a random code. The functions takes in a string colors whose characters represent distinct colors to choose from, and a positive integer code_length representing the length of the code. For instance, get_code('ROYGBP',4) returns a code of 4 code pegs randomly with colors chosen from 'R'ed, 'O'range, 'Y'ellow, 'G'reen, 'B'lue, and 'P'urple. One possible outcome is 'ROYY'. Following is my python code, and I don't know how to continue, can someone help me to finished...
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.
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")...
Copy the following Python fuction discussed in class into your file: from random import * def...
Copy the following Python fuction discussed in class into your file: from random import * def makeRandomList(size, bound): a = [] for i in range(size): a.append(randint(0, bound)) return a a. Add another function that receives a list as a parameter and computes the sum of the elements of the list. The header of the function should be def sumList(a): The function should return the result and not print it. If the list is empty, the function should return 0. Use...
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have...
def sum_gt_avg(num_list): Implement a function that returns the sum of the numbers in num_list that have a value greater than the average value in the list. • Parameters: num_list is a list of numbers (mixed integers and floats) • Examples: sum_gt_avg([1,2,3,4,5]) → 9 # 4+5 sum_gt_avg([1,2,3,-4,5]) → 10 # 2+3+5 sum_gt_avg([-1,-2,-3,-4,-5]) → -3 # -1-2 in python
" line 48"     return delta.days     ^ SyntaxError: 'return' outside function import datetime def nowDate():...
" line 48"     return delta.days     ^ SyntaxError: 'return' outside function import datetime def nowDate():     current_time = datetime.datetime.now()     new_time = datetime.datetime(         current_time.year,         current_time.month,         current_time.day,         0,         0,         0,         )     return new_time def plusOneDay(daytime):     tomorrow = daytime + datetime.timedelta(days=1)     return tomorrow def nDaystoHoliday():     NJCU_holidays = [         (datetime.date(2019, 1, 1), "New Year's Day"),         (datetime.date(2019, 1, 21), 'Martin Luther King, Jr. Day'),         (datetime.date(2019, 2, 19), "President's Day"),...
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 =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT