2017-04-25 66 views
0

所以我有一個函數count_by_type(db, stype)我必須計算有多少單類型和雙類型的口袋妖怪,類型匹配stype存在,對它們進行計數,然後將其求和成int 。函數返回一組ints而不是添加整數

給定一個叫做分貝格式,像這樣字典:

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返回。

編輯:其實我回來了一個單一的整數,我需要以符合設置符號的方式返回值。對於那個很抱歉。

回答

0

我設法解決這個問題,愚蠢的錯誤:你所要做的就是創建一個變量並將其設置爲一個元組。所以在這種情況下,它將是totaltuple =((single_type_count,dual_type_count,total_count)

相關問題