Ask Question
28 November, 20:43

Implement a class to represent a playing card. Your class should have the following methods: __ init _ (self, rank, suit) rank is an int in the range 1-13 indicating the ranks ace-king' and suit is a single character "d ' " "c' " "h ' " or "s" indicating the suit (diamonds, clubs, hearts, or spades). Create the corresponding card. getRank (self) Returns the rank of the card. get Suit (self) Returns the suit of the card. value (self) Returns the Blackjack value of a card. Ace counts as 1, face cards count as 10. __ str __ (self) Returns a string that names the card. For example, "Ace of Spades". Test your card class with a program that prints out n randomly generated cards and the associated Blackjack value where n is a number supplied by the user.

+3
Answers (1)
  1. 29 November, 00:06
    0
    from random import randrange

    class Card ():

    def __init__ (self, rank, suit):

    self. rank = rank

    self. suit = suit

    def getRank (self):

    return self. rank

    def getSuit (self):

    return self. suit

    def value (self):

    if self. getRank () < 10:

    return self. rank

    else:

    return 10

    def __str__ (self):

    ranks = [None, "Ace", "Two", "Three", "Four", "Five", "Six",

    "Seven", "Eight","Nine","Ten","Jack", "Queen", "King"]

    rankStr = ranks[self. rank]

    if self. suit = = 'c':

    suitStr = "Clubs"

    elif self. suit = = 'd':

    suitStr = "Diamonds"

    elif self. suit = = 'h':

    suitStr = "Hearts"

    else:

    suitStr = "Spades"

    return "{0} of {1}". format (rankStr, suitStr)

    def main ():

    n = int (input ("How many cards would you like to see? "))

    for i in range (n):

    rank = randrange (1,14)

    suit = "dchs"[randrange (4) ]

    randCard = Card (rank, suit)

    print (randCard, "counts as", randCard. value ())

    if __name__ = = '__main__':

    main ()
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Implement a class to represent a playing card. Your class should have the following methods: __ init _ (self, rank, suit) rank is an int in ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers