PieCharmer
PieCharmer

Reputation: 17

Dictionary item not returning correct value

I'm creating a function that returns a proper field name based on a number that is input. The test script is just returning the first key, not the desired value (1).

def GetFieldName(bldgcode):
    bldgcode = {1: "SFCount", 2: "MFCount", 3: "ComCount", 4: "IndCount", 5: "CityCount", 6: "ShedCount", 7: "SchCount", 8: "ChurCount"}
    for values in bldgcode:
        return values
#test for get field name
print(GetFieldName(8))

Running the script is giving me 1 when it should be ChurCount

Upvotes: 0

Views: 44

Answers (1)

Sinon
Sinon

Reputation: 131

How about directly use dict[key] to get name?

def GetFieldName(bldgcode):
    code2name = {1: "SFCount", 2: "MFCount", 3: "ComCount", 4: "IndCount", 5: "CityCount", 6: "ShedCount", 7: "SchCount", 8: "ChurCount"}
    return code2name[bldgcode]
#test for get field name
print(GetFieldName(8))

Upvotes: 2

Related Questions