我的程序应该获取每个月的降雨量,并计算当年的总降雨量、月平均降雨量以及降雨量最高(最大)和最低(最小)的月份。
除最高月和最低月的产出外,一切都按计划进行。我需要这个来显示降雨量最高和最低的月份的名称。我可以获得要显示的正确值,只是不是月份的名称。
months = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"]
values = []
year = []
for i in months :
values.append(float(input("Enter total rain for " + i + ": ")))
print()
def total():
print("The total rainfall for the year is %.2f" % sum(values))
total()
def average():
print("The average monthly rainfall is %.2f" % float(sum(values)/ 12))
average()
def highest():
print("The highest monthly rainfall is", max(values))
highest()
def lowest():
print("The lowest monthly rainfall is", min(values))
lowest()
您可以 zip
values
与 months
并获取 min
或 max
,作为 (值、月)
。确保将值放在首位,以便用于排序。
>>> months = ["January", "February", "March", "April",
... "May", "June", "July", "August",
... "September", "October", "November", "December"]
>>> values = [47, 2, 28, 9, 4, 64, 28, 94, 1, 9, 4, 23] # or whatever
>>> val, month = min(zip(values, months))
>>> print("The lowest rainfall was %r in %s" % (val, month))
The lowest rainfall was 1 in September
我是一名优秀的程序员,十分优秀!