首页 > 解决方案 > Python Shell 不返回任何内容

问题描述

这是我认为应该可以工作的下面的代码。当我调用它时,python shell 返回 empty(blank) 并且上面会弹出另一个 Restart 行。想知道如何解决这个问题?

此函数问题的说明如下: 描述:编写一个名为 animal_locator 的函数,该函数接受一个包含动物园位置作为键的字典,它们的值是具有特定动物和该动物园中该特定动物种群的元组列表。您应该返回一个字典,其中包含动物作为键,它们的值是一个元组,它们的第一个元素是所有动物园位置的有序列表,基于每个位置有多少动物(从最大到最少),第二个元素是整数该特定动物的总人口。您不必考虑区分大小写。

def animal_locator(places):
newdict = {}
for city in places:
    numtup = len(places[city])
    num = 0
    while num < numtup:
        if places[city][num][0] not in newdict:
            newlist = []
            newtup = (places[city][num][1], city)
            newlist.append(newtup)
            for city1 in places:
                if city1 != city:
                    for tup in places[city1]:
                        if tup[0] == places[city][num][0]:
                            tupnew = (tup[1], city1)
                            newlist.append(tupnew)
            newlist.sort(reverse=True)
            count = 0
            newlist2 = []
            for tup in newlist:
                newlist2.append(tup[1])
                count += tup[0]
                newtup = (newlist2, count)
            newdict[places[city][num][0]] = newtup
    num += 1
return newdict

zoo_location1 = {'San Diego': [('lion', 4), ('tiger', 2), ('bear', 8)], 'Bronx': [('lion', 20), ('snake', 5), ('tiger', 1)], 'Atlanta': [('lion', 3), ('snake', 2), ('bee', 4500)], 'Orlando': [('bee', 234), ('tiger', 123)]}
animal_dict1 = animal_locator(zoo_location1)
print(animal_dict1)

标签: python

解决方案


我发现我的 num += 1 行需要缩进一个制表符,然后它才能正常运行。


推荐阅读