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 the range

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. getSuit(self) Returns the suit of the card. BJValue(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". Note: A method named __str__ is special in Python. If asked to convert an object into a string, Python uses this method, if it's present. For example: c= Card(1, "s") print c will print "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.

i created the coding but I am getting an error. Please help me fix it.

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 -= rank[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)

from random import randrange

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() # Call the main procedure declared above

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!