Q: I would like to fix an error in this class Hand from a program in Python whic
ID: 3831766 • Letter: Q
Question
Q: I would like to fix an error in this class Hand from a program in Python which says below: What does no attribute means? And how to integrate hand in this class? If can please assist.
# 'Hand' object has no attribute 'hand'
class Hand:
def __init__(self, id):
self .id = id.strip()
self.cards = []
def addCard(self, card):
if not card in self.cards:
self.cards.append(card)
def showHand(self):
print(self)
def __str__(self):
result = self.id + ": "
for card in self.cards:
result += str(card) + " "
return result
Explanation / Answer
What does no attribute means?
It means that you are trying to access ".hand" using an instance of Hand class but Hand does not have any method named hand also it doesn't have any attribute (instance variable) named hand.
Seems like you haven't provided full code, here is my code, try it and see if it works for you.
import random
class Card:
def __init__(self, rank, suite):
rank = rank.strip()
self._rank = rank
self.rank = rank
if rank == 'A':
self.rank = 14
elif rank == 'J':
self.rank = 11
elif rank == 'Q':
self.rank = 12
elif rank == 'K':
self.rank = 13
else:
self.rank = int(rank)
self.suite = suite.strip()
def __lt__(self,other):
return int(self.rank) < int(other.rank)
def __le__(self,other):
return int(self.rank) <= int(other.rank)
def __gt__(self,other):
return int(self.rank) > int(other.rank)
def __ge__(self,other):
return int(self.rank) >= int(other.rank)
def __eq__(self,other):
return int(self.rank) == int(other.rank)
def __ne__(self,other):
return not(self.__eq__)
def __str__(self):
return (self._rank + " " + self.suite)
class Deck:
'represents a deck of 52 cards'
# ranks and suits are Deck class variables
ranks = {'2','3','4','5','6','7','8','9','10','J','Q','K','A'}
# suits is a set of 4 Unicode symbols representing the 4 suits
suits = {'u2660', 'u2661', 'u2662', 'u2663'}
def __init__(self):
'initialize deck of 52 cards'
self.deck = [] # deck is initially empty
for suit in Deck.suits: # suits and ranks are Deck
for rank in Deck.ranks: # class variables
# add Card with given rank and suit to deck
self.deck.append(Card(rank,suit))
def dealCard(self):
'deal (pop and return) card from the top of the deck'
return self.deck.pop()
def shuffle(self):
'shuffle the deck'
random.shuffle(self.deck) #not doing anything; should randomize a list?
class Hand:
def __init__(self, id):
self .id = id.strip()
self.cards = []
def addCard(self, card):
if not card in self.cards:
self.cards.append(card)
def showHand(self):
print(self)
def __str__(self):
result = self.id + ": "
for card in self.cards:
result += str(card) + " "
return result
hand = Hand( ' House ')
deck = Deck()
deck.shuffle()
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.addCard(deck.dealCard())
hand.showHand()
print(hand)
# code link: https://paste.ee/p/1q9FI
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.