python - 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)
解决方案
我发现我的 num += 1 行需要缩进一个制表符,然后它才能正常运行。
推荐阅读
- python - 在 Maya 的 liveMode 中从用户启动的 QuadDraw 工具中查询一个值
- c++ - 使用 GTK 将 EGL 图像渲染到帧缓冲区
- npm - 如何检查 NPM 包名称是否可用?
- azure - 运行由 VS2019 创建的 Azure Docker 容器注册表
- google-api - GMail API 用户速率在较长时间内受到限制
- python - 为什么 cf.go_offline 在 pyCharm 中不起作用
- java - org.springframework.data.domain.PageImpl 不能转换为
- javascript - 如何在 ReactJS 中更改状态?
- javascript - 在 Discord.js 中实例化嵌入对象时出错
- html - 如何使用 html 和 css 制作苹果手表网格?