Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In many card games, cards are either face up or face down. Add a new instance variable named faceup to the Card class to track

In many card games, cards are either face up or face down. Add a new instance variable named faceup to the Card class to track this attribute of a card. Its default value is False. Then add a turn method to turn the card over. This method resets the faceup variable to its logical negation.

importrandom. This is the code I currently have and it has an error where it says: AttributeError: 'Card' object has no attribute 'turn'. How can it be fixed?

classCard(object):

"""Acardobjectwithasuitandrank."""

RANKS=(1,2,3,4,5,6,7,8,9,10,11,12,13)

SUITS=('Spades','Diamonds','Hearts','Clubs')

def__init__(self,rank,suit,faceup=False):

"""Createsacardwiththegivenrankandsuit."""

self.rank=rank

self.suit=suit

self.faceup=faceup

def__str__(self):

"""Returnsthestringrepresentationofacard."""

ifself.rank==1:

rank='Ace'

elifself.rank==11:

rank='Jack'

elifself.rank==12:

rank='Queen'

elifself.rank==13:

rank='King'

else:

rank=self.rank

returnstr(rank)+'of'+self.suit

defturn(self):

self.faceup=notself.faceup

importrandom

classDeck(object):

"""Adeckcontaining52cards."""

def__init__(self):

"""Createsafulldeckofcards."""

self.cards=[]

forsuitinCard.SUITS:

forrankinCard.RANKS:

c=Card(rank,suit)

self.cards.append(c)

defshuffle(self):

"""Shufflesthecards."""

random.shuffle(self.cards)

defdeal(self):

"""RemovesandreturnsthetopcardorNone

ifthedeckisempty."""

iflen(self)==0:

returnNone

else:

returnself.cards.pop(0)

def__len__(self):

"""Returnsthenumberofcardsleftinthedeck."""

returnlen(self.cards)

def__str__(self):

"""Returnsthestringrepresentationofadeck."""

result=''

forcinself.cards:

result=self.result+str(c)+' '

returnresult

defmain():

"""Asimpletest."""

deck=Deck()

print("Anewdeck:")

whilelen(deck)>0:

print(deck.deal())

deck=Deck()

deck.shuffle()

print("Adeckshuffledonce:")

whilelen(deck)>0:

print(deck.deal())

card=Card(1,'Spades')

print(" test1",card.faceup)

card.turn()

print(" test2",card.faceup)

card.turn()

print(" test2",card.faceup)

if__name__=="__main__":

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

The components of a brand

Answered: 1 week ago