为什么我总是获取AttributeError:“set”对象没有属性“get”?

2024-05-26 20:45:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从输入到函数中的一只手数出每一套衣服的数量。这是我当前的代码:

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)

但是,我一直在获取错误AttributeError:“set”对象没有属性“get”


Tags: 函数代码get数量ifdefupdatespade
1条回答
网友
1楼 · 发布于 2024-05-26 20:45:17

当你做hands = {''}的时候,你是在做一个集,而不是一个字典。用hands = {}替换它就可以了

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)

更多信息请参见on the python documentation

相关问题 更多 >

    热门问题