221,291 views
30 votes
30 votes
Write the definition of a class named "Card" which creates a card from a standard deck of cards. This class should have the following methods: __init__(self, rank, suit), getRank(self), getSuit(self), printCard(self), shuffle(self), and cheat(self, rank, suit).

• The suits are: "Hearts", "Diamonds", "Clubs", and "Spades".
• The ranks are: "Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", and "King".
• getRank(self) and getSuit(self) return the rank and the suit of the card, respectively.
• printCard(self) prints the information on the card, like "This card is the Ace of Spades."
• shuffle(self) replaces the card with any card from the deck, chosen randomly.
• cheat(self, rank, suit) changes the rank and suit of the card to the input given in cheat.

The user should be able to do the following (also see template below):
c = Card("Ace", "Spade")
c.getRank()
"Ace"
c.getSuit()
"Spade"
c.printCard()
This card is the Ace of Spades.

c.shuffle()
c.printCard()
This card is the Ten of Diamonds. # Or any other random card

c.cheat("King", "Heart")
c.printCard()
This card is the King of Hearts.

c.cheat("12", "Spades")
c.printCard()
Invalid card

from random import choice

# Definition of class Card

# Note that the possible suits are ["Hearts", "Diamonds", "Clubs", "Spades"]
# The possible ranks are ["Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

class Card:
def __init__(self, rank, suit):
self.rank =
self.suit =

def getRank(self):
# Enter your code here

def getSuit(self):
# Enter your code here

def printCard(self):
# Enter your code here

def shuffle(self):
# You will need a list of the possible suits and a list of the possible ranks to be able to shuffle
# Recall that the method choice(list) chooses a random element from the list.
# Enter your code here

def cheat(self, rank, suit):
# Enter your code here
# If you have time, you should check that the rank and suit entered are valid by checking if they are in the lists of possible ranks and suits.

c = Card("Ace", "Spade")
c.getRank()
c.getSuit()
c.printCard()

c.shuffle()
c.printCard()

c.cheat("King", "Heart")
c.printCard()

c.cheat("12", "Spades")
c.printCard()

Note: The program has to be written in the Python programming language!

User Joseph Quinsey
by
2.7k points

1 Answer

12 votes
12 votes

import random

class Card:

def __init__(self, rank, suit):

# Initialize the rank and suit of the card

self.rank = rank

self.suit = suit

def getRank(self):

# Return the rank of the card

return self.rank

def getSuit(self):

# Return the suit of the card

return self.suit

def printCard(self):

# Print the information on the card

print(f"This card is the {self.rank} of {self.suit}.")

def shuffle(self):

# Define a list of possible ranks and suits

ranks = ["Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

suits = ["Hearts", "Diamonds", "Clubs", "Spades"]

# Assign a random rank and suit to the card

self.rank = random.choice(ranks)

self.suit = random.choice(suits)

def cheat(self, rank, suit):

# Change the rank and suit of the card to the input given in cheat

self.rank = rank

self.suit = suit

# Example usage:

card = Card("Ace", "Spades")

card.printCard() # Output: This card is the Ace of Spades.

card.shuffle()

card.printCard() # Output: This card is the random rank and suit.

card.cheat("King", "Hearts")

card.printCard() # Output: This card is the King of Hearts.

User Salva Corts
by
3.0k points