n 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. Note: The program should output in the following format: A new deck, cards face down: Ace of Spades False 2 of Spades False 3 of Spades False ... ... ... Jack of Spades False Queen of Spades False King of Spades False Ace of Diamonds False 2 of Diamonds False 3 of Diamonds False ... ... ... A deck shuffled once, cards face up: 9 of Diamonds True 6 of Clubs True King of Clubs True King of Hearts True 3 of Spades True ... ----------------------------------------------------------------------------------- """ File: cards.py Project 9.8 Module for playing cards, with classes Card and Deck. Adds a faceup attribute and a turn method. """ import random class Card(object): """ A card object with a suit and rank.""" RANKS = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13) SUITS = ('Spades', 'Diamonds', 'Hearts', 'Clubs') def __init__(self, rank, suit): """Creates a card with the given rank and suit.""" self.rank = rank self.suit = suit def __str__(self): """Returns the string representation of a card.""" if self.rank == 1: rank = 'Ace' elif self.rank == 11: rank = 'Jack' elif self.rank == 12: rank = 'Queen' elif self.rank == 13: rank = 'King' else: rank = self.rank return str(rank) + ' of ' + self.suit import random class Deck(object): """ A deck containing 52 cards.""" def __init__(self): """Creates a full deck of cards.""" self.cards = [] for suit in Card.SUITS: for rank in Card.RANKS: c = Card(rank, suit) self.cards.append(c) def shuffle(self): """Shuffles the cards.""" random.shuffle(self.cards) def deal(self): """Removes and returns the top card or None if the deck is empty.""" if len(self) == 0: return None else: return self.cards.pop(0) def __len__(self): """Returns the number of cards left in the deck.""" return len(self.cards) def __str__(self): """Returns the string representation of a deck.""" result = '' for c in self.cards: result = self.result + str(c) + '\n' return result def main(): """A simple test.""" deck = Deck() print("A new deck:") while len(deck) > 0: print(deck.deal()) deck = Deck() deck.shuffle() print("A deck shuffled once:") while len(deck) > 0: print(deck.deal()) if __name__ == "__main__": main()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.

Note: The program should output in the following format:

A new deck, cards face down:

Ace of Spades False

2 of Spades False

3 of Spades False

...

...

...

Jack of Spades False

Queen of Spades False

King of Spades False

Ace of Diamonds False

2 of Diamonds False

3 of Diamonds False

...

...

...

A deck shuffled once, cards face up:

9 of Diamonds True

6 of Clubs True

King of Clubs True

King of Hearts True

3 of Spades True

...

-----------------------------------------------------------------------------------

 

"""
File: cards.py
Project 9.8
Module for playing cards, with classes Card and Deck.
Adds a faceup attribute and a turn method.
""" 
import random

class Card(object):
    """ A card object with a suit and rank."""

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

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

    def __init__(self, rank, suit):
        """Creates a card with the given rank and suit."""
        self.rank = rank
        self.suit = suit
        
    def __str__(self):
        """Returns the string representation of a card."""
        if self.rank == 1:
            rank = 'Ace'
        elif self.rank == 11:
            rank = 'Jack'
        elif self.rank == 12:
            rank = 'Queen'
        elif self.rank == 13:
            rank = 'King'
        else:
            rank = self.rank
        return str(rank) + ' of ' + self.suit

import random

class Deck(object):
    """ A deck containing 52 cards."""

    def __init__(self):
        """Creates a full deck of cards."""
        self.cards = []
        for suit in Card.SUITS:
            for rank in Card.RANKS:
                c = Card(rank, suit)
                self.cards.append(c)

    def shuffle(self):
        """Shuffles the cards."""
        random.shuffle(self.cards)

    def deal(self):
        """Removes and returns the top card or None 
        if the deck is empty."""
        if len(self) == 0:
           return None
        else:
           return self.cards.pop(0)

    def __len__(self):
       """Returns the number of cards left in the deck."""
       return len(self.cards)

    def __str__(self): 
        """Returns the string representation of a deck."""
        result = ''
        for c in self.cards:
            result = self.result + str(c) + '\n'
        return result

def main():
    """A simple test."""
    deck = Deck()
    print("A new deck:")
    while len(deck) > 0:
        print(deck.deal())
    deck = Deck()
    deck.shuffle()
    print("A deck shuffled once:")
    while len(deck) > 0:
        print(deck.deal())

if __name__ == "__main__":
    main()
    
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education