Andrew N. Andrew N. - 1 year ago 120
Python Question

Python lists and function, finding the max/min months of rainfall

My program is supposed to take the rainfall amounts for each month and calculate the total rainfall for the year, the average monthly rainfall, and the months with the highest(max) and lowest(min) rainfall amounts.

Everything works as planned, except for the highest and lowest month outputs. I need for this to display the name of the months with the highest and lowest rainfall amounts. I can get the correct values to display, just not the name of the month.

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 + ": ")))

def total():
print("The total rainfall for the year is %.2f" % sum(values))

def average():
print("The average monthly rainfall is %.2f" % float(sum(values)/ 12))

def highest():
print("The highest monthly rainfall is", max(values))

def lowest():
print("The lowest monthly rainfall is", min(values))

Answer Source

You can find the index of the min/max values and then use that index to find the month. Using a zip is the more pythonic way to do it, but here is another beginner's approach

def highest():
    print("The highest rainfall month is:",months[values.index(max(values))] )
    print("The highest monthly rainfall is", max(values))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download