Andy
Andy

Reputation: 13

Why do I keep getting AttributeError: 'set' object has no attribute 'get'?

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

Answers (1)

Ayush Garg
Ayush Garg

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

Related Questions