0

ok the guy helpes me with my problem in my last post but I need to switch this:

#This module determines highest value in list
def highestMonthNumber (rainfall):
month = ['January','Febuary','March','April','May','June','July','August'\
,'September','October','November','December']

highestMonthly = 0
for m, n in enumerate(rainfall):
if n > highestMonthly:
highestMonthly = n
highestMonth = m
return month[highestMonth], highestMonthly

to find lowest........

the rest of the code is in my other post including what he fixed for me

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by woooee
0

You could write it this way

MONTH = ['January','Febuary','March','April','May','June','July','August',
            'September','October','November','December']

def highestMonth (rainfall):
    bestMonth, bestValue = 0, rainfall[0]
    for month, value in enumerate(rainfall):
        if value > bestValue:
            bestMonth, bestValue = month, value
    return MONTH[bestMonth], bestValue

def lowestMonth (rainfall):
    bestMonth, bestValue = 0, rainfall[0]
    for month, value in enumerate(rainfall):
        if value < bestValue:
            bestMonth, bestValue = month, value
    return MONTH[bestMonth], bestValue

Note that if you couldn't modify the previous code to find the lowest monthly rainfall, it means that you don't really understand what the code does. If you want to learn python, you must understand what each of these lines of code does exactly. You could add print statements to display the values of the variables, etc.

Edited by Gribouillis: n/a

0

Lists have min and max functions which could also be used. For both code solutions, what happens if there is more than one month with the same rainfall?

def lowest_month (rainfall):
    lowest = min(rainfall)
    return MONTH[rainfall.index(lowest)], lowest

Edited by woooee: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.