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...
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?
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
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...
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches...
Python Coding 1. validate_username_password(username, password, users): This function checks if a given username and password matches in the stored users dictionary. This function returns True if a match found otherwise returns False. 2. validate_existing_user(users): This function asks for username and password and checks if user provided name and password matches. It prints an informational message and returns username if it does find a match. Call validate_username_password() function to perform this validation. A user has total of three chances to validate....
Write python 3 code to define a function that uses three arguments, and returns a result....
Write python 3 code to define a function that uses three arguments, and returns a result. The function returns True if the first argument is more than or equal to the second argument and less than or equal to the third argument, otherwise it returns False. Write a python 3 code for function main, that does the following: creates a variable and assign it the value True. uses a while loop which runs as long as the variable of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT