Programming using Python Filename: cards.py In this program, you will create functions that use lists to create, shuffle, and deal cards to a number up to 4 “hands”. The standard deck of playing cards contains 52 cards. Each card has one of four suits along with a value. Suits are normally spades, hearts, diamonds and clubs. The values are 2 through 10, Jack, Queen, King and Ace (we will ignore the “Joker”). Each card will be represented using two characters in a string. The first character is the value of the card, with 2 through 9 represented directly. The other cars with letters: “T”, “J”, “Q”, “K”, and “A” represent the values 10, Jack, Queen, King, and Ace respectively. The second character in the string represents the suit of the card. These are: “s” for spades, “h” for hearts, “d” for diamonds, and “c” for clubs. Here’s an example: In addition to any other functions you decide to write, you MUST provide the following functions that we can run with our test suite. The names, return type and parameter types must match (of course, you can name the parameters anything you wish). Note: You are NOT providing a test suite. Make sure your function names & argument types match the descriptions below. create_deck(): Creates a deck of 52 cards using the abbreviations outlined in the requirements. Input parameters: None. Return value: a list of cards, not shuffled. If we specified this with Python annotations, the signature would be: create_deck() -> list: shuffle(cards): Shuffles the 52 cards in a new list, such that the list of cards is ordered randomly, while leaving the original list of 52 cards unchanged. Shuffle returns the “shuffled deck” to the caller. Input parameters: a list of cards, not shuffled. Return value: a list of cards, shuffled. The original list is not modified (aka “mutated”) If we specified this with Python annotations, the signature would be: shuffle(cards : list) -> list: deal(number_of_hands, number_of_cards, cards): Takes the number of hands (players) (up to 4), number of cards per hand/player (up to 13), and the deck of cards to deal from. Your function should return a list containing all of the hands that were dealt. Each hand will be a represented as a list of cards. Therefore, this function will return a list of lists. As a side-effect, the cards dealt should be removed from deck passed as an input parameter to this function. Input parameters: number of hands -> int, number of cards -> int, cards -> list. *** Note: It's perfectly fine to rename the "hands" parameter to "players" if you wish to, especially if that makes it more clear to you *** Return value: a list of hands/players, where each hand is itself a list of cards dealt for a player. If we specified this with Python annotations, the signature would be: deal(number_of_hands : int, number_of_cards : int, cards : list) -> list: For this function, assume PRECONDITION: number of cards has been pre-validated, values allowed: 0..13; number of hands has been pre-validated, values allowed: 1..4 Also keep in mind that deal() returns a nested list, not a single list! Example tests we might run with your functions: (in photo attached named "exampleofcards")
- Filename: cards.py
In this program, you will create functions that use lists to create, shuffle, and deal cards to a number up to 4 “hands”.
The standard deck of playing cards contains 52 cards. Each card has one of four suits along with a value. Suits are normally spades, hearts, diamonds and clubs. The values are 2 through 10, Jack, Queen, King and Ace (we will ignore the “Joker”).
Each card will be represented using two characters in a string. The first character is the value of the card, with 2 through 9 represented directly. The other cars with letters: “T”, “J”, “Q”, “K”, and “A” represent the values 10, Jack, Queen, King, and Ace respectively.
The second character in the string represents the suit of the card. These are: “s” for spades, “h” for hearts, “d” for diamonds, and “c” for clubs.
Here’s an example:
In addition to any other functions you decide to write, you MUST provide the following functions that we can run with our test suite. The names, return type and parameter types must match (of course, you can name the parameters anything you wish).
Note: You are NOT providing a test suite.
Make sure your function names & argument types match the descriptions below.
- create_deck(): Creates a deck of 52 cards using the abbreviations outlined in the requirements.
Input parameters: None. Return value: a list of cards, not shuffled.
If we specified this with Python annotations, the signature would be: create_deck() -> list:
- shuffle(cards): Shuffles the 52 cards in a new list, such that the list of cards is ordered randomly, while leaving the original list of 52 cards unchanged. Shuffle returns the “shuffled deck” to the caller.
Input parameters: a list of cards, not shuffled. Return value: a list of cards, shuffled. The original list is not modified (aka “mutated”)
If we specified this with Python annotations, the signature would be: shuffle(cards : list) -> list:
- deal(number_of_hands, number_of_cards, cards): Takes the number of hands (players) (up to 4), number of cards per hand/player (up to 13), and the deck of cards to deal from. Your function should return a list containing all of the hands that were dealt. Each hand will be a represented as a list of cards. Therefore, this function will return a list of lists. As a side-effect, the cards dealt should be removed from deck passed as an input parameter to this function.
Input parameters: number of hands -> int, number of cards -> int, cards -> list.
*** Note: It's perfectly fine to rename the "hands" parameter to "players" if you wish to, especially if that makes it more clear to you ***
Return value: a list of hands/players, where each hand is itself a list of cards dealt for a player.
If we specified this with Python annotations, the signature would be:
deal(number_of_hands : int, number_of_cards : int, cards : list) -> list:
For this function, assume PRECONDITION:
number of cards has been pre-validated, values allowed: 0..13;
number of hands has been pre-validated, values allowed: 1..4
Also keep in mind that deal() returns a nested list, not a single list!
Example tests we might run with your functions:
(in photo attached named "exampleofcards")
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 4 images