Python:For循环使用索引而不是嵌套循环进行2列输出

时间:2013-11-02 07:19:10

标签: python indexing nested-loops

我希望我的程序看起来像这样,但是当我运行程序时出现错误。我不知道我做错了什么请帮助我。非常感谢。对不起我的英语不好

降雨量统计

Month           Total Rainfall
-----           ---------------
Jan                  10
Feb                  20
Mar                  15
Apr                  5
May                  4
Jun                  5
Jul                  3
Aug                  2
Sep                  8
Oct                  7
Nov                  10
Dec                  12

Total Rainfall:      96
Average Rainfall:    8.0

这是我的代码:

amount = []
total = 0

month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
for num in range(1,13):
    am = int(raw_input("Enter amount of rainfall each month from Jan to Dec sequently: "))
    amount.append(am)
    total+=am

print "\nRainfall Statistics"
print "Month\t\tTotal Rainfall"
print "-----\t\t---------------"

for index in month:
    print month[index], "\t\t", amount[index]

print "\ntotal rainfall: ", total

average = total/12
print "\nAverage rainfall: ",average

这是我的输出:

Enter amount of rainfall each month from Jan to Dec sequently: 1
Enter amount of rainfall each month from Jan to Dec sequently: 2
Enter amount of rainfall each month from Jan to Dec sequently: 3
Enter amount of rainfall each month from Jan to Dec sequently: 4
Enter amount of rainfall each month from Jan to Dec sequently: 5
Enter amount of rainfall each month from Jan to Dec sequently: 6
Enter amount of rainfall each month from Jan to Dec sequently: 7
Enter amount of rainfall each month from Jan to Dec sequently: 8
Enter amount of rainfall each month from Jan to Dec sequently: 9
Enter amount of rainfall each month from Jan to Dec sequently: 10
Enter amount of rainfall each month from Jan to Dec sequently: 11
Enter amount of rainfall each month from Jan to Dec sequently: 12

Rainfall Statistics
Month       Total Rainfall
-----       ---------------

错误:

Traceback (most recent call last):
  File "/Users/matter_neverdie/Desktop/python/rainFall.py", line 15, in <module>
    print month[index], "\t\t", amount[index]
TypeError: list indices must be integers, not str

3 个答案:

答案 0 :(得分:3)

Python与JavaScript之类的其他语言不同,forin循环遍历数组的元素而不是数组元素的索引。你的意思是

for index in range(len(month)):

答案 1 :(得分:0)

首先,在你的输入循环中,写入会更好 for num in range(12)将循环12个tiems并为你提供索引0..11。这比你的解决方案更受青睐,因为你不必考虑你真正拥有的hov mny iterationy。

你可以对第二个循环使用完全相同的想法:

for index in range(12):

会起作用,但你可以通过使用len()计算列表的长度(也许你想要两年后等等)来改善这一点。你应该写

之前
for index in range( len(month) ): #or len(amount)

在python中,您还可以使用内置的enumerate在一行中获取列表的索引和元素。 有了这个,您可以执行以下操作:

for index, month in enumerate(month):
    print month, "\t\t", amount[index]

最后一个例子可能不是你在作业中所做的事情; o)......

答案 2 :(得分:0)

您的程序应该看起来与此相似

from itertools import izip

amount = []
total = 0

month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
idx = 0
while(idx < len(month)):
    am = int(raw_input("Enter amount of rainfall each month from Jan to Dec sequently: "))
    amount.append(am)
    total+=am
    idx+=1

print "\nRainfall Statistics"
print "Month\t\tTotal Rainfall"
print "-----\t\t---------------"

for each_month, month_amount in izip(month, amount):
    print each_month, "\t\t", month_amount

print "\nTotal rainfall: ", total

average = total/12
print "\nAverage rainfall: ",average

当你在Python中使用for循环遍历列表时,你会得到元素,而不是像循环的C样式那样的数字。

在迭代多个列表时,最好使用itertools模块中存在的工具,比如izip,它具有许多优点,而不是迭代一个列表并使用数字索引迭代另一个。

您还应该看到何时需要在Python中使用for循环和while循环。尽管它们可能在C语言中起到相同的作用,但它们可以用来完成Python中的不同事物。这个link可能会帮助您何时使用for循环以及何时使用while循环。