Question

In: Computer Science

Use python write a function that checks the result of the blackjack game, given the sum...

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([], [])

    []

    """

Solutions

Expert Solution

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:


Related Solutions

Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A...
6. Write a Python function that checks whether a passed string is palindrome or not. Note:-A palindrome is a word, phrase, or sequence that reads the same backward as forward . Some examples you may try: “madam” redder “race car” Eva, Can I Stab Bats In A Cave? If the argument passed is not a string, invoke an exception or an assertion and state in a comment which one you have chosen and why.
Parentheses Checking by python use a suitable class to write a function, given that the input...
Parentheses Checking by python use a suitable class to write a function, given that the input is the string of parentheses, check if they have the matching open and close brackets
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack,...
PYTHON BEGINNER Problem Create a program that lets the user play a simplified game of Blackjack, which is played between the user and an automated dealer as follows. The dealer shuffles a standard deck of 52 cards, draws two cards, and gives them to the user. The user can then choose to request another card from the dealer, adding it to their hand. The user can continue to request cards or choose to stop at any time. After each time...
1. write a function to represent the volume of the box given that the sum of...
1. write a function to represent the volume of the box given that the sum of the height and perimeter of the base is equal to 240 inches. 2. what dimensions (lenght width height) of such a box will give the maximum volume? 3. what is the maximum volume?
Making a blackjack game in javascript Create a function called createDeck() that will create an array...
Making a blackjack game in javascript Create a function called createDeck() that will create an array of objects (52). Each object contains two properties: suit and value. The suit property will be either 'Hearts', 'Clubs', 'Diamonds', or 'Spades'. The value property will be either 'Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 'Four', 'Three' , or 'Two''. Note: We will probably want to store possible suits and values in separate arrays. Create a function called shuffleDeck() that will...
Simplify the following logical function using Karnaugh Maps. You will write the result as a sum...
Simplify the following logical function using Karnaugh Maps. You will write the result as a sum of products. Do not leave blank spaces in the expression. Write the literals of the terms in alphabetical order. For example, instead of writing the term acb 'write ab'c. Write the function starting with the term that has the fewest literals, and then proceeding in ascending order of literals per term. That is, if for example the simplified function has a term with 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT