Reputation: 13
I'm trying to count the number of each specific suit from a hand inputted into the function. This is my current code:
def tally(h):
hands = {''}
hands.update(h)
for i in h:
if hands.get('♥'):
heart += 1
if hands.get('♣'):
club += 1
if hands.get('♦'):
diamond +=1
if hands.get('♠'):
spade += 1
suit = {
'♥':heart,
'♣':club,
'♦':diamond,
'♠':spade
}
return(suit)
but, I keep getting the error AttributeError: 'set' object has no attribute 'get'?
Upvotes: 1
Views: 769
Reputation: 2517
When you do hands = {''}
, you are making a set, not a dictionary. Replacing this with hands = {}
does the trick.
def tally(h):
hands = {}
hands.update(h)
for i in h:
if hands.get('♥'):
heart += 1
if hands.get('♣'):
club += 1
if hands.get('♦'):
diamond +=1
if hands.get('♠'):
spade += 1
suit = {
'♥':heart,
'♣':club,
'♦':diamond,
'♠':spade
}
return(suit)
More information can be found on the python documentation.
Upvotes: 4