【发布时间】:2015-03-26 18:01:20
【问题描述】:
设计一个程序,让用户将 12 个月中每个月的总降雨量输入到一个列表中。该程序应计算并显示当年的总降雨量、月平均降雨量以及最高和最低降雨量的月份。
def main():
January = requestNumber ("Enter the amount of rainfall for the month of January:")
February = requestNumber ("Enter the amount of rainfall for the month of February:")
March = requestNumber ("Enter the amount of rainfall for the month of March:")
April = requestNumber ("Enter the amount of rainfall for the month of April:")
May = requestNumber ("Enter the amount of rainfall for the month of May:")
June = requestNumber ("Enter the amount of rainfall for the month of June:")
July = requestNumber ("Enter the amount of rainfall for the month of July:")
August= requestNumber ("Enter the amount of rainfall for the month of August:")
September = requestNumber ("Enter the amount of rainfall for the month of September:")
October = requestNumber ("Enter the amount of rainfall for the month of October:")
November = requestNumber ("Enter the amount of rainfall for the month of November:")
December = requestNumber ("Enter the amount of rainfall for the month of December:")
Year = [January, February, March, April, May, June, July, August, September, October, November, December]
total_rainfall = (sum(Year))
showInformation("The total rainfall for the year is:" + str(total_rainfall))
monthly_average = (sum(Year)) / 12
showInformation ("The monthly average for the year is:" + str(monthly_average))
这是我到目前为止的代码。我真的在努力解决问题的最后一部分,即显示降雨量最高和最低的月份。我知道如何使用最大和最小来显示数字,但我不知道如何获得与最大和最小降雨量相对应的月份。
感谢任何人都可以给我的任何帮助,这是在 Jython 中。
【问题讨论】:
-
将值存储为字典
{January : 12}。那么你就可以轻松读取列表中的values,然后得到最低和最高