Question

In: Computer Science

Write a function called play_round that simulates two people drawing cards and comparing their values. High...

Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. the function has two parameters: the name of player 1 and the name of player 2. it returns a string with format ' wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'. python

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

Note: Since you did not provide any existing code or info about cards, I'm just using random numbers between 1 and 52 to simulate cards (you may change 52 to 13 if you want), or let me know if you want to integrate some existing code into this new method.

#code with no comments, for easy copying

import random


def play_round(player1, player2):
    c1 = random.randint(1, 52)
    c2 = random.randint(1, 52)
    while c1 == c2:
        c1 = random.randint(1, 52)
        c2 = random.randint(1, 52)
    if c1 > c2:
        return player1 + ' wins!'
    else:
        return player2 + ' wins!'



if __name__ == '__main__':
    for i in range(5):
        print(play_round("oliver","sara"))

#same code with comments, for learning

import random


# required method, taking two player names
def play_round(player1, player2):
    # generating a card value between 1 and 52 for player 1
    c1 = random.randint(1, 52)
    # repeating the same for player 2
    c2 = random.randint(1, 52)
    # looping as long as both values are same
    while c1 == c2:
        # re drawing the cards
        c1 = random.randint(1, 52)
        c2 = random.randint(1, 52)
    # at the end, if c1>c2, player1 wins, else player2 wins
    if c1 > c2:
        return player1 + ' wins!'
    else:
        return player2 + ' wins!'


# test code, for calling play_round 5 times and print results
# remove if not needed
if __name__ == '__main__':
    for i in range(5):
        print(play_round("oliver", "sara"))

#output

sara wins!
oliver wins!
sara wins!
sara wins!
oliver wins!

Related Solutions

In python please write the following code the problem. Write a function called play_round that simulates...
In python please write the following code the problem. Write a function called play_round that simulates two people drawing cards and comparing their values. High card wins. In the case of a tie, draw more cards. Repeat until someone wins the round. The function has two parameters: the name of player 1 and the name of player 2. It returns a string with format '<winning player name> wins!'. For instance, if the winning player is named Rocket, return 'Rocket wins!'.
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
Write a function called is_equal(). This function will take as an argument two integers. Call the...
Write a function called is_equal(). This function will take as an argument two integers. Call the is_equal function and supply two integers. You do not need to get the integers from the keyboard. Compare the two integers. If the two integers are equal, then print "equal". If the two integers are not equal, print "not equal". Call the function from your main code to execute the function. Sample output: equal Sample output: not equal
Write a function called swapElements to swap two elements in a character array. This function takes...
Write a function called swapElements to swap two elements in a character array. This function takes two parameters, a character array and input file. The function should read two numbers from a file and it swap the elements stored in the indices read. please code in c++
JavaScript Write a function called "first" that takes in two arguments - the first is an...
JavaScript Write a function called "first" that takes in two arguments - the first is an argument called arr that is an array of numbers, the second is an optional number argument called num(hint: you'll need a default value - look back at the slides). If "num" was not passed in (since it's optional), the "first" function will return an array containing the first item of the array. If a value for "num" was given, the "first" function will return...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data...
Write a Matlab function called: lin_interp. The function should have three inputs: the two original data arrays (call them x and f), and the array you would like to interpolate to (call it xstar). The function should have one output: the interpolated array ( call it fstar). The function should be able to interpolate x and f onto xstar using linear interpolation and give the result as fstar. The function may not use any intrinsic functions except length.
1) Compute the probability of drawing two clubs from a standard deck of 52 cards. Round...
1) Compute the probability of drawing two clubs from a standard deck of 52 cards. Round your answer to four decimal places. Answer: Compute the probability of drawing two face cards from a standard deck of 52 cards. Round your answer to four decimal places. Answer: Compute the probability of drawing two aces from a standard deck of 52 cards. Round your answer to four decimal places. Answer:
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT