In: Computer Science
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!'.
Here is the complete code for the given function in Python. I have added comments for a better understanding of the same:
import random
def play_round(player1, player2):
while(True):
c1 = random.randint(1,13) # Player 1 draws a card
c2 = random.randint(1,13) # Player 2 draws a card
if(c1 < c2):
return player2 + ' wins!' # Player 2 has higher card
elif(c2 < c1):
return player1 + ' wins!' # Player 1 has higher card
Sample Run:
You can comment below the answer in case of any doubts and I will be happy to help.
Please give a thumbs up if the answer could be of help!
All the best!