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.
Write a C program called cards.c that simulates some card game logic by comparing the cards...
Write a C program called cards.c that simulates some card game logic by comparing the cards from 4 people and determining which person has the best card. The program MUST work as follows: Eachcardmustberepresentedbyexactlytwocharsrepresenting a rank and a suit. The possible rank options are: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. The possiblesuit options are: 'H', 'D', 'S', 'C'. So 6H represents the “6 of hearts”, JC represents the “Jack of Clubs” etc... YouMUSTwriteafunctioncalledisValidRank(charc)which...
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 Matlab function called “SimpleChannel” which takes a signal as an input, simulates a channel...
Write a Matlab function called “SimpleChannel” which takes a signal as an input, simulates a channel and returns the transmitted signal.. SimpleChannel function will add a randomly generated noise (look for “rand” function) to the signal and return it. You can adjust the amplitude of the noise as you want in order to have a distinct noise effect. Then write another matlab code to show the channel noise. First create the signal and then simulate the channel by using your...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that...
Write a function cube_all_lc(values) that takes as input a list of numbers called values, and that uses a list comprehension to create and return a list containing the cubes of the numbers in values (i.e., the numbers raised to the third power). This version of the function may not use recursion.
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 program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
Write a python function comparing two binary trees and returns whethere they are same. Input two...
Write a python function comparing two binary trees and returns whethere they are same. Input two binary tress and output true or false. True= They are the same, False= They are not . Test the function .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT