In: Computer Science
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
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!