This week you learned the basic principles of objectoriented

This week you learned the basic principles of object-oriented programming, and how classes model real-world objects.

Now you can use this knowledge to simulate playing-cards.

- Implement a class called Card to represent a playing card. The class should have the following methods:

__init__(self, rank, suit)  - to create a new card. rank is an int in the range 1-13 indicating the ranks from Ace (1) to King (13). suit is a single character - \'d\', \'c\', \'h\', or \'s\' indicating the suit (diamonds, clubs, hearts, or spades)

getRank(self) - returns the card\'s rank

getSuit(self) - returns the card\'s suit

BJValue(self) - returns the card\'s Blackjack value (Ace = 1, face cards = 10)

__str__(self) - returns the card\'s full name (e.g. \"Ace of Spades\")

- Save your class in a file called card.py

- Other programs should be able to import your class and create a card like this:

Hint

Your __str__ method will need some way to match card rank and suit values with longer form values. Consider whether a lists or a dictionary is better for each.

Extra Credit

Write a function in card.py, but not part of the Card class, that asks the user whether to draw a card. If the user agrees, the function should create a new card with randomly selected value and suit. The program should display the combined total of Blackjack values after each draw, and prompt the user to draw another card until the total exceeds 21.

Solution

from random import randrange

class Card:

    \'\'\'create card class\'\'\'

    def __init__(self,rank,suit):

self.rank = rank

if self.rank > 10:

self.value = 10

else:

self.value = self.rank

if suit == 1 : self.suit = \'Diamonds\'

elif suit == 2: self.suit = \'Clubs\'

elif suit == 3: self.suit = \'Hearts\'

elif suit == 4: self.suit = \'Spades\'

if self.rank == 1 : self.nName = \'Ace\'

elif self.rank == 2 : self.nName = \'Two\'

elif self.rank == 3 : self.nName = \'Three\'

elif self.rank == 4 : self.nName = \'Four\'

elif self.rank == 5 : self.nName = \'Five\'

elif self.rank == 6 : self.nName = \'Six\'

elif self.rank == 7 : self.nName = \'Seven\'

elif self.rank == 8 : self.nName = \'Eight\'

elif self.rank == 9 : self.nName = \'Nine\'

elif self.rank == 10 : self.nName = \'Ten\'

elif self.rank == 11 : self.nName = \'Jack\'

elif self.rank == 12 : self.nName = \'Queen\'

elif self.rank == 13 : self.nName = \'King\'

def __str__(self):

return self.nName+\' of \'+self.suit

def BJvalue(self):

\'\'\'define self.value and return it\'\'\'     

return self.value

def getRank(self):

\'\'\'return rank of this card\'\'\'

return self.rank

def getSuit(self):

\'\'\'return suit of this card\'\'\'

return self.suit

def main():

\'\'\'Main algorithm\'\'\'

printIntro()

n,test = getInput()

if test != \'error\':

for i in range(n):

card = drawCard()

printCardInfo(i,card)

def printIntro():

\'\'\'Prin program name.\'\'\'

print(\'Drawing card program.\ \')

def getInput():

\'\'\'Ask user input and verify it\'\'\'

    

n = test = 0

try:

n = eval(input(\'How many card do you need? \'))

print()

except ValueError:

print(\'\ Error! Only integer number acceptable.\')

test = \'error\'

except NameError:

print(\'\ Error! Only integer number acceptable.\')

test = \'error\'

except:

print(\'Unknow error!\')

test = \'error\'

if test != \'error\':

n,test = verifyInput(n,test)

return n,test

def verifyInput(n,test):

\'\'\'Verify user input that he\'s not enter something to break program\'\'\'

    if n <= 0:

test = \'error\'

print(\'\ Error!. You must provide positive integer number.\')      

return n,test

    

def drawCard():

\'\'\'Draw cards many as user want.\'\'\'

rank,suit = genCardValue()

x = Card(rank,suit)

return x

def genCardValue():

\'\'\'randomly generate rank between 1-13 and suit between 1-4\'\'\'

r = randrange(1,14)

s = randrange(1,5)

return r,s

def printCardInfo(i,card):

\'\'\'Print card detail.\'\'\'

print(\'Card\',i+1,\'is \',card,\'which has value of\',card.BJvalue())

if __name__ == \'__main__\': main()

This week you learned the basic principles of object-oriented programming, and how classes model real-world objects. Now you can use this knowledge to simulate
This week you learned the basic principles of object-oriented programming, and how classes model real-world objects. Now you can use this knowledge to simulate
This week you learned the basic principles of object-oriented programming, and how classes model real-world objects. Now you can use this knowledge to simulate
This week you learned the basic principles of object-oriented programming, and how classes model real-world objects. Now you can use this knowledge to simulate

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site