下午好!我对Python相对比较陌生 - 我正在为一个类进行作业。Python3 - 建议使用范围while循环?
此代码的目标是下载文件,向文件中添加一行数据,然后创建一个循环遍历每行数据,并打印出城市名称和最高平均温度该城市的数据。
我的代码如下 - 我有输出工作,没问题。我遇到的唯一问题是IndexError: list index out of range
- 最后。
我已经在StackOverflow上进行了搜索 - 以及使用Python在线挖掘range()函数文档。我想我只需要适当地理解范围(),并且我会完成它。
如果我拿出范围,我得到同样的错误 - 所以我试图改变for/in为 - 在mean_temps城市: 其结果是,输出只显示7个城市中的4个 - 跳过其他所有城市。
任何意见将不胜感激! 这里是我的代码 - 下面的截图链接显示输出和错误,以及:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
mean_temps = open('mean_temp.txt', 'a+')
mean_temps.write("Rio de Janeiro,Brazil,30.0,18.0")
mean_temps.seek(0)
headings = mean_temps.readline().split(',')
print(headings)
while mean_temps:
range(len(city_temp))
for city in mean_temps:
city_temp = mean_temps.readline().split(',')
print(headings[0].capitalize(),"of", city_temp[0],headings[2], "is", city_temp[2], "Celsius")
mean_temps.close()
欢迎来到这个网站!我很高兴你参加了这次巡演。你会看到[如何问问页面](https://stackoverflow.com/help/how-to-ask)然后[编辑你的问题](https://stackoverflow.com/posts/45597960/编辑)相应?例如,请将内容添加到源代码中,而不是添加到屏幕截图中。如果人们可以复制代码并自行尝试,他们更有可能帮助您。谢谢,祝你好运! – cxw
好的,会做...谢谢! –
除了抛出'NameError'之外,你还期望'range(len(city_temp))'做什么? –