-3

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

降雨统计

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
4

3 回答 3

3

Python 与 JavaScript 等其他语言不同,forin循环遍历数组元素,而不是数组元素的索引。你本来打算

for index in range(len(month)):
于 2013-11-02T07:24:54.983 回答
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 风格的 for 循环那样的数字。

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

您还应该了解何时需要在 Python 中使用 for 循环和 while 循环。尽管它们在 C 等语言中可能具有相同的目的,但它们可用于在 Python 中完成不同的事情。此链接可能会帮助您了解何时使用 for 循环以及何时使用 while 循环。

于 2013-11-02T08:00:31.873 回答
0

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

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

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)...

于 2013-11-02T07:46:58.813 回答