Question

Implement a class named PlayStack. An instance of this class should have a property cards, which...

  1. Implement a class named PlayStack. An instance of this class should have a property cards, which is a list of ordered cards that behaves like a stack. This property should not be directly accessible from outside the class. An instance of this class should behave like a stack, but it is a special stack as you will see below. You can only add cards from one end.
  2. Implement a method peekValue() which returns the value of the card on top of cards. Raise an Exception "Error: No cards in the playing stack" when appropriate.
  3. Implement a method peekFace() which returns the face of the card on top of cards. Raise an Exception "Error: No cards in the playing stack" when appropriate.
  4. Implement a method named playCard(card), that takes a card and pushes it on top of the cards stack. The card with value 0 can only be added when the cards stack is empty, and the method only accepts valid cards from the Card class (only numbers between 0-9 are accepted cards). Also, an added card can only be pushed on top of a card of directly lower value. For example, a card of value 5 can be added only on a card of value 4; Only a card of value 8 can be pushed on top of a card of value 7. If an error occurs the method should raise an Exception "Error: Card rejected". When the card of value 9 is played and accepted, the stack should be emptied and the method returns a list of the faces of all cards that were in the stack. Otherwise, the method returns an empty list.
  5. Implement the __str__() method to convert a PlayStack instance into a string such that the string represents the cards in the stack from the lowest value on the left to the highest value on the right delimited by two "|", eg. |[0][1][*][3][4][5]|.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ans:

# the required class design is as follows:

# OUTPUT FOR THIS SAMPLE RUN!

# ANOTHER SAMPLE RUN:

# CORRESPONDING OUTPUT TO THE SAMPLE RUN:

# REQUIRED PROGRAM:

import random

class PlayStack:
  
def __init__(self):
  
self.cards = list()
  
def peekValue(self):
  
try:
card = self.cards[-1]
return card
except:
return "Error: No cards in the playing stack"
  
def peekFace(self):
  
try:
card = self.cards[-1]
return random.choice(["Heart","Spade","Club","Diamond"])
except:
return "Error: No cards in the playing stack"
  
def playCard(self,card):
  
try:
top = self.cards[-1]
if card == 9:
return self.__str__()
elif card == top + 1 and card > 0 and card < 9:
self.cards.append(card)
return "Appended Successfully"
elif card == 0:
return "Card 0 can only be added on an empty deck!"
else:
return "Error: Card {} rejected".format(card)
except:
if card == 9:
return self.__str__()
else:
self.cards.append(card)
return "Appended Successfully"
  
def __str__(self):
  
if len(self.cards) == 0:
return "[]"
  
else:
s = ""
for i in self.cards:
s += "[" + str(i) + "]"
return "|{}|".format(s)
  
  
# card object of class PlayStack
cards = PlayStack()
# testing the __str__ method on empty deck
print(cards)
# checking peekValue on empty deck
print(cards.peekValue())
# Inserting on empty deck
print(cards.playCard(0))
print(cards.peekValue())
print(cards.peekFace())
print(cards.playCard(1))
print(cards.playCard(2))
print(cards.playCard(3))
print(cards.playCard(5))
print(cards.playCard(9))

  

# PLEASE DO LIKE AND UPVOTE IF THIS WAS HELPFUL!

# THANK YOU SO MUCH IN ADVANCE!

Add a comment
Know the answer?
Add Answer to:
Implement a class named PlayStack. An instance of this class should have a property cards, which...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT