In: Computer Science
Use python write a function that checks the result of the blackjack game, given the sum of all cards in player 1’s and player 2’s hand in each round. For the i-th round, the i-th index of player1 list represents the sum of all cards of player1, and the i-th index of player2 list represents player2's card sum. The i-th index of the returned list should be the winner's card sum. If both players' card sums go over 21 in a round, we put 0 for that round to indicate that no one wins instead. You can assume two input lists always have the same length.
Example:
If player1’s list is [19, 25, 23], player2’s list is [20, 15, 25], the returned list should be [20, 15, 0]. Since in the first round, 20 is greater than 19 but not exceeds 21; in the second round, 25 exceeds 21 but 15 doesn’t; and in the third round, both 23 and 25 exceed 21.
def check_Blackjack(player1, player2):
"""
>>> check_Blackjack([19, 21, 22], [21, 19, 3])
[21, 21, 3]
>>> check_Blackjack([17, 21, 22, 29], [15, 19, 3, 4])
[17, 21, 3, 4]
>>> check_Blackjack([], [])
[]
"""
def check_Blackjack(player1, player2):
'''
Function to find the maximum sum from two given list such that
the sum is less than or equal to 21.
'''
totalRounds = len(player1)
# list to store the winner sum
WinnerList = []
# iterating over all rounds of the game
for rounds in range(totalRounds):
# if both player have sum = 21
if player1[rounds] > 21 and player2[rounds] > 21:
WinnerList.append(0)
# if player1's sum is greater than player2
elif player1[rounds] > player2[rounds]:
# if player1's sum is not greater than 21
if player1[rounds] <= 21:
WinnerList.append(player1[rounds])
else:
WinnerList.append(player2[rounds])
# if player2's sum is greater than player1
elif player2[rounds] >= player1[rounds]:
# if player1's sum is not greater than 21
if player2[rounds] <= 21:
WinnerList.append(player2[rounds])
else:
WinnerList.append(player1[rounds])
# return the winner sum list
return WinnerList
# Sample run:
def main():
print(check_Blackjack([19, 25, 23], [20, 15, 25]))
print(check_Blackjack([19, 21, 22], [21, 19, 3]))
print(check_Blackjack([17, 21, 22, 29], [15, 19, 3, 4]))
print(check_Blackjack([], []))
if __name__ == '__main__':
main()
Screenshot of program for understanding indentation:
Sample run results: