Question

In: Computer Science

Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...

Instructions

Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue.

Include a shebang, and ID header, descriptive comments, and use constants where appropriate.

Sample Output (user input in red):

Welcome to the card dealer!
Here is your first card:
5 of hearts
Would you like another card? (y or n) y
King of hearts
Would you like another card? (y or n) y
Jack of clubs
Would you like another card? (y or n) y
King of spades
Would you like another card? (y or n) n
final hand value = 35
final hand: ['5 of hearts', 'King of hearts', 'Jack of clubs', 'King of spades']

Submission

Attach and submit your Python file to this assignment.

Solutions

Expert Solution

Python3 code:

#!/usr/bin/env python3

# Import the module
import random

# Global variables
value = 0
drawn = []
LETTER_CARD_VALUE = 10

# function to compute the total of the cards drawn
def compute_total(card):
    global value

    number = card.split()[0]
    if len(number) == 1:
        value += int(number)
    else:
        value += LETTER_CARD_VALUE


# function to draw a card from the deck
def draw_one():
    global drawn

    # list of 52 cards
    cards = ["King of hearts", "Queen of hearts", "Jack of hearts", "Ace of hearts", "1 of hearts", "2 of hearts", "3 of hearts", "4 of hearts", "5 of hearts", "6 of hearts", "7 of hearts", "8 of hearts", "9 of hearts",
             "King of clubs", "Queen of clubs", "Jack of clubs", "Ace of clubs", "1 of clubs", "2 of clubs", "3 of clubs", "4 of clubs", "5 of clubs", "6 of clubs", "7 of clubs", "8 of clubs", "9 of clubs",
             "King of spades", "Queen of spades", "Jack of spades", "Ace of spades", "1 of spades", "2 of spades", "3 of spades", "4 of spades", "5 of spades", "6 of spades", "7 of spades", "8 of spades", "9 of spades",
             "King of diamonds", "Queen of diamonds", "Jack of diamonds", "Ace of diamonds", "1 of diamonds", "2 of diamonds", "3 of diamonds", "4 of diamonds", "5 of diamonds", "6 of diamonds", "7 of diamonds", "8 of diamonds", "9 of diamonds"]

    # remove drawn cards from the 52 cards
    remaining_cards = list(set(cards)-set(drawn))

    # randomly select one card
    card = remaining_cards[random.randint(0, len(remaining_cards) - 1)]
    drawn.append(card)

    # call the function
    compute_total(card)

    return card

# main function
def main():

    # draw first card
    print("Here is your first card:")
    print(draw_one())

    # loop to ask the user whether he wants to draw the card
    while 1:
        c = input("Would you like another card? (y or n) ")

        if c == 'y':
            print(draw_one())
        elif c == 'n':
            break
        
    # print the output
    print("final hand value =", value)
    print("final hand:", drawn)
    

if __name__ == "__main__":
    main()

Please refer to the following pictures for the source code and its indentation:

Sample execution of the above code:


Related Solutions

Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a...
Instructions Use your solution from Programming Assignment 5 (or use your instructor's solution) to create a modular Python application. Include a main function (and a top-level scope check),and at least one other non-trivial function (e.g., a function that deals the new card out of the deck). The main function should contain the loop which checks if the user wants to continue. Include a shebang, and ID header, descriptive comments, and use constants where appropriate. Sample Output (user input in red):...
Using your solution or your instructor's solution from the Module 8 ungraded practice exercise, implement a...
Using your solution or your instructor's solution from the Module 8 ungraded practice exercise, implement a unit test for the Pair class by adding a main method. Create three small constituent classes (I created PeanutButter, Jelly, and Mustard classes) and insert various combinations of them into an array of Pair objects. Thoroughly test the equals(), hashCode(), and toString() methods from the Pair class with your main method. Note: override the toString() method in each of your constituent classes so they...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going...
Assignment 1 – Writing a Linux Utility Program Instructions For this programming assignment you are going to implement a simple C version of the UNIX cat program called lolcat. The cat program allows you to display the contents of one or more text files. The lolcat program will only display one file. The correct usage of your program should be to execute it on the command line with a single command line argument consisting of the name you want to...
The programming language is Python Instructions: Create a function that will delete a node in a...
The programming language is Python Instructions: Create a function that will delete a node in a Linked List based on position number. On below example, if you want to delete position #2, it will remove the Banana (arrangement of nodes below is Apple, Banana, Cherry, Grapes, Orange). myLinkedList = LinkedList() myLinkedList.append("Banana") myLinkedList.append("Cherry") myLinkedList.append("Grapes") myLinkedList.append("Orange") myLinkedList.prepend("Apple") myLinkedList.deleteByPositionNum(2) node = myLinkedList.head while node: print(node.value, " ") node = node.next_node You may start with the function head: def deleteByPositionNum(self, positionNum):
Code in C Instructions For this programming assignment you are going to implement a simulation of...
Code in C Instructions For this programming assignment you are going to implement a simulation of Dijkstra’s solution to the Dining Philosophers problem using threads, locks, and condition variables. Dijkstra’s Solution Edsgar Dijkstra’s original solution to the Dining Philosophers problem used semaphores, but it can be adapted to use similar mechanisms: • Each philosopher is in one of three states: THINKING, HUNGRY, or EATING. • Every philosopher starts out in the THINKING state. • When a philosopher is ready to...
From question 19: findAndTruncate "...+2 EC (at the instructor's discretion) will go to the (correct) solution...
From question 19: findAndTruncate "...+2 EC (at the instructor's discretion) will go to the (correct) solution with the least number of statements ." Find two unneeded statements and one performance issue in the below code: findAndTruncate(char *trunc, const char placeholder) { for(int i = 0; *(trunc + i) != '\0'; i++)          { if(*(trunc + i) == placeholder)                    {    * (trunc + i) = '\0'; } } }
For this programming assignment, you will use your previous code that implemented a video game class...
For this programming assignment, you will use your previous code that implemented a video game class and objects with constructors. Add error checking to all your constructors, except the default constructor which does not require it. Make sure that the high score and number of times played is zero or greater (no negative values permitted). Also modify your set methods to do the same error checking. Finally add error checking to any input requested from the user. #include <iostream> #include...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...
Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through...
You will create this assignment following the Assignment Detail instructions below. Review the tutorial titled How...
You will create this assignment following the Assignment Detail instructions below. Review the tutorial titled How to Submit the Intellipath Submission Assignment. Please submit your work to this week’s Intellipath Unit Submission lesson. Click the Upload button within the submission lesson to access the submission area. Click the Select File button to upload your document, and then click "OK" to finish. Assignment Details Throughout this class, you have been working with a product of your creation that dealt domestically. This...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT