函数返回一组int而​​不是添加int

时间:2017-04-25 02:39:04

标签: python dictionary for-loop sum tuples

所以我有一个函数count_by_type(db, stype),我必须计算有多少匹配stype类型的单一和双重型Pokemon存在,计算它们,然后将它加总成一个int。

给定一个名为db的dict,其格式如下:

    sample_db = {
"Bulbasaur": (1, "Grass", "Poison", 45, 49, 49, 45, 1, False),
"Charmander": (4, "Fire", None, 39, 52, 43, 65, 1, False),
"Charizard": (6, "Fire", "Flying", 78, 84, 78,100, 1, False),
"Moltres": (146, "Fire", "Flying", 90,100, 90, 90, 1, True),
"Crobat": (169, "Poison", "Flying", 85, 90, 80,130, 2, False),
"Tornadus, (Incarnate Form)": (641, "Flying", None, 79,115, 70,111, 5, True),
"Reshiram": (643, "Dragon", "Fire", 100,120,100, 90, 5, True)
}

我实现了一些代码来完成上面描述的操作。这里:

def count_by_type(db, stype):
    single_type_count = 0
    dual_type_count = 0
    total_count = 0
    for pokemon in db:
        if (db[pokemon][1] == stype and db[pokemon][2] != stype) or (db[pokemon][1] != stype and db[pokemon][2] == stype):
            single_type_count += 1
        if (db[pokemon][1]== stype or db[pokemon][2] == stype):
            dual_type_count += 1
total_count = single_type_count + dual_type_count
return total_count

问题是它返回一个像(1,2,3)或(4,0,4)这样的集合而不是添加计数器,因此它将分别返回6或8。

编辑:实际上我返回了一个整数,我需要以与设置符号相匹配的方式返回值。对不起。

1 个答案:

答案 0 :(得分:0)

我自己设法解决这个问题,愚蠢的错误:你所要做的就是创建一个变量并将其设置为等于元组。所以在这种情况下,它将是totaltuple =((single_type_count,dual_type_count,total_count)

相关问题