The program below has an error. The error occurs in section 15.6 of this program
ID: 3619984 • Letter: T
Question
The program below has an error. The error occurs in section 15.6 of this program. It should print out all of the cards in the deck like this >>> deck = Deck() >>> print deck Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs Jack of Clubs Queen of Clubs King of Clubs Ace of Diamonds here is the error I get Traceback (most recent call last): File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 68, in <module> print deck File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 63, in __str__ for i in range(len(self.cards)): AttributeError: Deck instance has no attribute 'cards' here is the program with the error. The error is in section 15.7 I will award you 140 points if you fix this program and make it run with no errors in Python IDLE ---------------------------------------------# -*- coding: utf-8 -*- # 15.2 class Card: def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank threeOfClubs = Card(3, 1)
# 15.3 class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank #init method omitted def __str__(self): return (self.rankList[self.rank] + " of " + self.suitList[self.suit]) card1 = Card(1, 11) print card1
card2 = Card(1, 3) print card2 print card2.suitList[1] card1.suitList[1] = "Swirly Whales" print card1 print card2
# 15.4 def __cmp__(self, other): # check the suits if self.suit > other.suit: return 1 if self.suit < other.suit: return -1 # suits are the same... check ranks if self.rank > other.rank: return 1 if self.rank < other.rank: return -1 # ranks are the same... it’s a tie return 0
# 15.5 class Deck: def __init__(self): self.cards = [] for suit in range(4): for rank in range(1, 14): self.cards.append(Card(suit, rank))
# 15.6 class Deck: # ... def printDeck(self): for card in self.cards: print card
class Deck: # ... def __str__(self): s = "" for i in range(len(self.cards)): s = s + " "*i + str(self.cards[i]) + " " return s
deck = Deck() print deck
# 15.7 random.randrange(0, len(self.cards)) class Deck: # ... def shuffle(self): import random nCards = len(self.cards) for i in range(nCards): j = random.randrange(i, nCards) self.cards[i], self.cards[j] = self.cards[j], self.cards[i] self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
# 15.8 class Deck: # ... def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: return False
class Deck: # ... def popCard(self): return self.cards.pop()
class Deck: # ... def isEmpty(self): return (len(self.cards) == 0)
# 15.9 Glossary # # Encode: To represent one set of values using another set of values by constructing # a mapping between them. # # Class Attribute: A variable that is de?ned inside a class de?nition but outside # any method. Class attributes are accessible from any method in the class # and are shared by all instances of the class. # # Accumulator: A variable used in a loop to accumulate a series of values, such as # by concatenating them onto a string or adding them to a running sum. # The program below has an error. The error occurs in section 15.6 of this program. It should print out all of the cards in the deck like this >>> deck = Deck() >>> print deck Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs Jack of Clubs Queen of Clubs King of Clubs Ace of Diamonds here is the error I get Traceback (most recent call last): File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 68, in <module> print deck File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 63, in __str__ for i in range(len(self.cards)): AttributeError: Deck instance has no attribute 'cards' here is the program with the error. The error is in section 15.7 I will award you 140 points if you fix this program and make it run with no errors in Python IDLE ---------------------------------------------
# -*- coding: utf-8 -*- # 15.2 class Card: def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank threeOfClubs = Card(3, 1)
# 15.3 class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank #init method omitted def __str__(self): return (self.rankList[self.rank] + " of " + self.suitList[self.suit]) card1 = Card(1, 11) print card1
card2 = Card(1, 3) print card2 print card2.suitList[1] card1.suitList[1] = "Swirly Whales" print card1 print card2
# 15.4 def __cmp__(self, other): # check the suits if self.suit > other.suit: return 1 if self.suit < other.suit: return -1 # suits are the same... check ranks if self.rank > other.rank: return 1 if self.rank < other.rank: return -1 # ranks are the same... it’s a tie return 0
# 15.5 class Deck: def __init__(self): self.cards = [] for suit in range(4): for rank in range(1, 14): self.cards.append(Card(suit, rank))
# 15.6 class Deck: # ... def printDeck(self): for card in self.cards: print card
class Deck: # ... def __str__(self): s = "" for i in range(len(self.cards)): s = s + " "*i + str(self.cards[i]) + " " return s
deck = Deck() print deck
# 15.7 random.randrange(0, len(self.cards)) class Deck: # ... def shuffle(self): import random nCards = len(self.cards) for i in range(nCards): j = random.randrange(i, nCards) self.cards[i], self.cards[j] = self.cards[j], self.cards[i] self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
# 15.8 class Deck: # ... def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: return False
class Deck: # ... def popCard(self): return self.cards.pop()
class Deck: # ... def isEmpty(self): return (len(self.cards) == 0)
# 15.9 Glossary # # Encode: To represent one set of values using another set of values by constructing # a mapping between them. # # Class Attribute: A variable that is de?ned inside a class de?nition but outside # any method. Class attributes are accessible from any method in the class # and are shared by all instances of the class. # # Accumulator: A variable used in a loop to accumulate a series of values, such as # by concatenating them onto a string or adding them to a running sum. # The program below has an error. The error occurs in section 15.6 of this program. It should print out all of the cards in the deck like this >>> deck = Deck() >>> print deck Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs Jack of Clubs Queen of Clubs King of Clubs >>> deck = Deck() >>> print deck Ace of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 9 of Clubs 10 of Clubs Jack of Clubs Queen of Clubs King of Clubs Ace of Diamonds here is the error I get Traceback (most recent call last): File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 68, in <module> print deck File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 63, in __str__ for i in range(len(self.cards)): AttributeError: Deck instance has no attribute 'cards' here is the program with the error. The error is in section 15.7 I will award you 140 points if you fix this program and make it run with no errors in Python IDLE File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 68, in <module> print deck File "G:/Class/Module 5/Module 5 Case/ThinkCSpy/Chapter 15/Chapter 15 examples new fixed.py", line 63, in __str__ for i in range(len(self.cards)): AttributeError: Deck instance has no attribute 'cards' here is the program with the error. The error is in section 15.7 I will award you 140 points if you fix this program and make it run with no errors in Python IDLE ---------------------------------------------
# -*- coding: utf-8 -*- # 15.2 class Card: def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank threeOfClubs = Card(3, 1)
# 15.3 class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank #init method omitted def __str__(self): return (self.rankList[self.rank] + " of " + self.suitList[self.suit]) card1 = Card(1, 11) print card1
card2 = Card(1, 3) print card2 print card2.suitList[1] card1.suitList[1] = "Swirly Whales" print card1 print card2
# 15.4 def __cmp__(self, other): # check the suits if self.suit > other.suit: return 1 if self.suit < other.suit: return -1 # suits are the same... check ranks if self.rank > other.rank: return 1 if self.rank < other.rank: return -1 # ranks are the same... it’s a tie return 0
# 15.5 class Deck: def __init__(self): self.cards = [] for suit in range(4): for rank in range(1, 14): self.cards.append(Card(suit, rank))
# 15.6 class Deck: # ... def printDeck(self): for card in self.cards: print card
class Deck: # ... def __str__(self): s = "" for i in range(len(self.cards)): s = s + " "*i + str(self.cards[i]) + " " return s
deck = Deck() print deck
# 15.7 random.randrange(0, len(self.cards)) class Deck: # ... def shuffle(self): import random nCards = len(self.cards) for i in range(nCards): j = random.randrange(i, nCards) self.cards[i], self.cards[j] = self.cards[j], self.cards[i] self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
# 15.8 class Deck: # ... def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: return False
class Deck: # ... def popCard(self): return self.cards.pop()
class Deck: # ... def isEmpty(self): return (len(self.cards) == 0)
# 15.9 Glossary # # Encode: To represent one set of values using another set of values by constructing # a mapping between them. # # Class Attribute: A variable that is de?ned inside a class de?nition but outside # any method. Class attributes are accessible from any method in the class # and are shared by all instances of the class. # # Accumulator: A variable used in a loop to accumulate a series of values, such as # by concatenating them onto a string or adding them to a running sum. # # -*- coding: utf-8 -*- # 15.2 class Card: def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank threeOfClubs = Card(3, 1)
# 15.3 class Card: suitList = ["Clubs", "Diamonds", "Hearts", "Spades"] rankList = ["narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] def __init__(self, suit=0, rank=2): self.suit = suit self.rank = rank #init method omitted def __str__(self): return (self.rankList[self.rank] + " of " + self.suitList[self.suit]) card1 = Card(1, 11) print card1
card2 = Card(1, 3) print card2 print card2.suitList[1] card1.suitList[1] = "Swirly Whales" print card1 print card2
# 15.4 def __cmp__(self, other): # check the suits if self.suit > other.suit: return 1 if self.suit < other.suit: return -1 # suits are the same... check ranks if self.rank > other.rank: return 1 if self.rank < other.rank: return -1 # ranks are the same... it’s a tie return 0
# 15.5 class Deck: def __init__(self): self.cards = [] for suit in range(4): for rank in range(1, 14): self.cards.append(Card(suit, rank))
# 15.6 class Deck: # ... def printDeck(self): for card in self.cards: print card
class Deck: # ... def __str__(self): s = "" for i in range(len(self.cards)): s = s + " "*i + str(self.cards[i]) + " " return s
deck = Deck() print deck
# 15.7 random.randrange(0, len(self.cards)) class Deck: # ... def shuffle(self): import random nCards = len(self.cards) for i in range(nCards): j = random.randrange(i, nCards) self.cards[i], self.cards[j] = self.cards[j], self.cards[i] self.cards[i], self.cards[j] = self.cards[j], self.cards[i]
# 15.8 class Deck: # ... def removeCard(self, card): if card in self.cards: self.cards.remove(card) return True else: return False
class Deck: # ... def popCard(self): return self.cards.pop()
class Deck: # ... def isEmpty(self): return (len(self.cards) == 0)
# 15.9 Glossary # # Encode: To represent one set of values using another set of values by constructing # a mapping between them. # # Class Attribute: A variable that is de?ned inside a class de?nition but outside # any method. Class attributes are accessible from any method in the class # and are shared by all instances of the class. # # Accumulator: A variable used in a loop to accumulate a series of values, such as # by concatenating them onto a string or adding them to a running sum. #