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

Write a function matchSuit() that simulates repeatedly drawing a pair of playing cards from a shuffled...
Write a function matchSuit() that simulates repeatedly drawing a pair of playing cards from a shuffled deck (of 52 cards) until the pair of cards share the same suit (or you run out of cards). The function should return the total number of cards drawn. Use a while loop. To draw each card, call the drawCard function you wrote above.
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!'.
Write a function election(numVoters) that simulates an election between two candidates A and B, with numVoters...
Write a function election(numVoters) that simulates an election between two candidates A and B, with numVoters voters, in which each voter randomly chooses candidate A 55% of the time. Return the fraction (between 0 and 1) of the voters who actually voted for candidate A in your simulation. finished in python
(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".
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT