I am not able to get the program below to work properly. I get this output: >>>
ID: 3620009 • Letter: I
Question
I am not able to get the program below to work properly. I get this output: >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> Its supposed to print out all of the different cards in a deck of cards. All 52 of them. instead it just prints <__main__.Deck instance at 0x026B6A80> My program code is below. Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.**************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.5 >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x025D6A80> >>> [DEBUG ON] >>> [DEBUG OFF] >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x027D6B98> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x02656A58> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> I am not able to get the program below to work properly. I get this output: >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> Its supposed to print out all of the different cards in a deck of cards. All 52 of them. instead it just prints <__main__.Deck instance at 0x026B6A80> My program code is below. Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
**************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.5 >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x025D6A80> >>> [DEBUG ON] >>> [DEBUG OFF] >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x027D6B98> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x02656A58> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> I am not able to get the program below to work properly. I get this output: >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> Its supposed to print out all of the different cards in a deck of cards. All 52 of them. instead it just prints <__main__.Deck instance at 0x026B6A80> My program code is below. Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
**************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.5 >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x025D6A80> >>> [DEBUG ON] >>> [DEBUG OFF] >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x027D6B98> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x02656A58> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>> Type "copyright", "credits" or "license()" for more information.
**************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 2.6.5 >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x025D6A80> >>> [DEBUG ON] >>> [DEBUG OFF] >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x027D6B98> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x02656A58> >>> ================================ RESTART ================================ >>> Jack of Diamonds 3 of Diamonds Diamonds Jack of Swirly Whales 3 of Swirly Whales <__main__.Deck instance at 0x026B6A80> >>>
Explanation / Answer
We have objects to represent Cards, to define a class to represent a Deck. Of course, a deck is made up of cards, so each Deck object will contain a list of cards as an attribute. The following is a class definition for the Deck class.
The initialization method creates the attribute cards and generates the standard set of fifty-two cards:
class Deck:
def constructor():
[property(Cards)] _Card as List
for Suit in range(4):
for Rank in range(1, 14):
Cards.Add (Card (Suit, Rank))
The easiest way to populate the deck is with a nested loop. The outer loop enumerates the suits from 0 to 3. The inner loop enumerates the ranks from 1 to 13. Since the outer loop iterates four times, and the inner loop iterates thirteen times, the total number of times the body is executed is fifty-two (thirteen times
four). Each iteration creates a new instance of Card with the current suit and rank, and appends that card to the cards list. The append method works on lists but not tuples.
Printing the Deck:
When we define a new type of object we want a method that prints the contents of an object. To print a Deck, we traverse the list and print each Card's Show method:
class Deck:
def constructor ():
[property(Cards)] _Card as List
for Suit in range(4):
for Rank in range(1, 14):
Cards.Add (Card (Suit, Rank))
def PrintDeck ():
for x as Card in Cards:
print x.Show
>>> deck = Deck()
>>> deck.PrintDeck ()
Ace 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
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Diamonds
Ace 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
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.