Answer To: Hello Looking for programming help and solutions to the 2 questions.
Sandeep Kumar answered on Mar 03 2021
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deck of Cards\n",
"\n",
"Please design two classes in this notebook as follows:\n",
"\n",
"1\\. Please create a class called **PlayingCard**. This class should have:
\n",
"- An attribute, \"rank\" that takes a value of \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"J\", \"Q\", \"K\", or \"A\"
\n",
"- An attribute, \"suit\" that takes a value of \"♠\" \"♥\" \"♦\" or \"♣\". (If you don't know how to make these characters you can cut and paste from this block)
\n",
"- An __init__ function that:\n",
" - Accepts as parameters a specific rank (as a string) and suit (as a string).\n",
" - Gives an appropriate response when a rank or suit is not valid.\n",
"\n",
"2\\. Please create a class called **Deck**. This class should have:
\n",
"- An attribute, \"cards\", that holds a list of PlayingCard objects.
\n",
"- An __init__ function that: \n",
"\n",
" - By default stores a full deck of 52 playing card (with proper numbers and suits) in the \"cards\" list. Each cards will be of the class PlayingCard above
\n",
" - Allows the user to specify a specific suit (of the 4 - \"♠\" \"♥\" \"♦\" or \"♣\"). In this case, the program should only populate the deck with the 13 cards of that suit.\n",
" - After the cards object is initialized, call the \"shuffle_deck()\" function (below).
\n",
" \n",
"- A \"shuffle_deck()\" function that randomly changes the order of cards in the deck.
\n",
" - You can import the random library to 'shuffle' the deck: https://docs.python.org/3.7/library/random.html\n",
" - If you import random, please import it at the top of your block instead of inside the class / methods.\n",
"\n",
"- A \"deal_card(card_count)\" function that removes the first card_count cards from the deck and returns them as a list.
\n",
" - Make sure this function gives an appropriate response when the deck is out of cards.\n",
" \n",
"3\\. You might have to write ```__str__ or __repr__``` methods to display the cards correctly in either or both classes.\n",
"\n",
"\n",
"Example:\n",
"```\n",
">>> card1 = PlayingCard(\"A\", \"♠\")\n",
">>> print(card1)\n",
"A of ♠\n",
"\n",
">>> card2 = PlayingCard(\"15\", \"♠\")\n",
"Invalid rank!\n",
"\n",
">>> card2 = PlayingCard(\"10\", \"bunnies\")\n",
"Invalid suit!\n",
"\n",
">>> deck1 = Deck()\n",
">>> print(deck1.cards)\n",
"[K of ♠, A of ♥, 6 of ♣, 7 of ♠, J of ♦, 6 of ♠, Q of ♦, 5 of ♣, 10 of ♦, 2 of ♥, 8 of ♣, 8 of ♦, 4 of ♦, 7 of ♦, 3 of ♣, K of ♣, 9 of ♠, 4 of ♥, 10 of ♥, 10 of ♣, A of ♠, 9 of ♥, 7 of ♥, 9 of ♣, 7 of ♣, 5 of ♠, 3 of ♦, 10 of ♠, Q of ♥, J of ♣, 5 of ♥, K of ♥, K of ♦, 2 of ♠, 8 of ♠, Q of ♣, 3 of ♠, 6 of ♥, 6 of ♦, A of ♣, A of ♦, 3 of ♥, J of ♠, 4 of ♣, 5 of ♦, 2 of ♦, 4 of ♠, 2 of ♣, Q of ♠, J of ♥, 8 of ♥, 9 of ♦] \n",
"\n",
">>> deck2 = Deck('♠')\n",
">>> deck2.shuffle_deck()\n",
">>> print(deck2.cards)\n",
"[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠, J of ♠, 9 of ♠, Q of ♠, 6 of ♠, 2 of ♠, K of ♠]\n",
"\n",
">>> deck2.deal_card(7)\n",
"[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠]\n",
"\n",
">>> deck2.deal_card(7)\n",
"Cannot deal 7 cards. The deck only has 6 cards left!\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# YOUR CODE HERE\n",
"import random\n",
"class PlayingCard():\n",
"\n",
" \n",
" def __init__(self, cardrank, cardsuit):\n",
" try:\n",
" if str(cardrank).upper() not in [\"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"J\", \"Q\", \"K\", \"A\"]:\n",
" raise Exception(\"Wrong rank! Please check again\")\n",
" elif cardsuit not in [\"♠\", \"♥\", \"♦\", \"♣\"]:\n",
" raise Exception(\"Wrong suit! Please check again\")\n",
" else:\n",
" self.rank = str(cardrank).upper()\n",
" self.suit = cardsuit\n",
" \n",
" except Exception as e:\n",
" print(str(e))\n",
" \n",
" def __str__(self):\n",
" return self.rank + \" of \" + self.suit\n",
" \n",
" def __repr__(self):\n",
" return self.rank + \" of \" + self.suit\n",
" \n",
" \n",
"class Deck():\n",
"\n",
" \n",
" def __init__(self, cardsuit=\"\"):\n",
" if cardsuit != \"\":\n",
" self.cards = [PlayingCard(x, cardsuit) for x in \n",
" [\"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"J\", \"Q\", \"K\", \"A\"]]\n",
" else:\n",
" self.cards = [PlayingCard(x, y) for x in [\"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"J\", \"Q\", \"K\", \"A\"]\n",
" for y in [\"♠\", \"♥\", \"♦\", \"♣\"]]\n",
" \n",
" self.shuffle_deck()\n",
" \n",
" def shuffle_deck(self):\n",
" random.shuffle(self.cards)\n",
" \n",
" def deal_card(self, card_count):\n",
" try:\n",
" self.__dealt_card = [] \n",
" if card_count <= len(self.cards):\n",
" for i in range(0, card_count): \n",
" self.__dealt_card.append(self.cards.pop(0))\n",
" print(self.__dealt_card)\n",
" \n",
" else:\n",
" ...