0

I am trying to figure out some homework for an intro to programming class. It seems I have gone the long way around finding most of the info. I have to design a program that takes a user input for total rainfall for each of the 12 months. It should calculate and display the total rainfall for the year, average rainfall, and the months with the highest and lowest amounts. I can get my program to do everything but display the month name with the highest and lowest. Like I mentioned before I know I have taken the long way on this and it is more than likely not the most efficient path. Here is my code.

year=[]
jan=float(input('Please enter Jan rainfall: '))
feb=float(input('Please enter Feb rainfall: '))
mar=float(input('Please enter Mar rainfall: '))
apr=float(input('Please enter Apr rainfall: '))
may=float(input('Please enter May rainfall: '))
jun=float(input('Please enter Jun rainfall: '))
jul=float(input('Please enter Jul rainfall: '))
aug=float(input('Please enter Aug rainfall: '))
sep=float(input('Please enter Sep rainfall: '))
oct=float(input('Please enter Oct rainfall: '))
nov=float(input('Please enter Nov rainfall: '))
dec=float(input('Please enter Dec rainfall: '))

year.extend((jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec))

def lowest():
    print('The minimum rainfall is', min(year))
lowest()

def highest():
    print('The most rainfall is', max(year))
highest()

def total():
    print('The total rainfall is', sum(year))
total()

def average():
    print('The average rainfall is', float(sum(year))/len(year))
average()

When I do this I get my input prompts, the min, the max, the total, and the average. I'm just not sure where to get the month names for the min and max. If there is a quicker way I'm all for it as well. Thanks.

FreddieB1027
  • 5
  • 1
  • 1
  • 3

5 Answers5

1

I would probably use a dict here:

year = {}
year['jan'] = float(input('Please enter Jan rainfall: '))
...

Now to get the minimum:

def lowest():
    print('The minimum rainfall is', min(year.values()))

lowest()

If you want to know what month had the lowest:

def lowest2():
    print('The month of minimum rainfall is', min(year, key=lambda x:year[x]))

lowest2()
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • Unfortunately the chapter this is from is about arrays/lists and does not go over dictionaries. So while it would probably be better, I can't really do it for this assignment if that makes sense. – FreddieB1027 Dec 09 '13 at 03:22
0

Since you can't use dictionaries, you could make a separate list for the month names:

months = ["jan", "feb", "mar", ... ]

Then use the index of the returned value in any given function to get the month name from the other list, e.g.

def lowest():
    low = min(year)
    month = months[year.index(low)]
    print ...
kaveman
  • 4,339
  • 25
  • 44
0

Edit: You added the requirement of no dictionaries, but I left my answer in case it is useful to others.

If you use a dict, you can do: https://stackoverflow.com/a/268285/341744

In [1]: months = {"jan":20, "feb:":32, "mar":45}

In [2]: max(months.iteritems(), key=operator.itemgetter(1))[0]
Out[2]: 'mar'

You can automate input, I only used two months for brevity.

In [3]: 
def input_rainfall(months):
    r = {}
    for month in months:
        while True:
            try:
                rain = float(input("Enter rainfall for {}: ".format(month)))
                r[month] = rain
                break
            except ValueError:
                print("You must enter a float")
    return r

months = input_rainfall(['jan', 'feb'])
Enter rainfall for jan: 3.4
Enter rainfall for feb: 3.41


In [4]: max(months.iteritems(), key=operator.itemgetter(1))[0]
Out[4]: 'feb'

In [5]: min(months.iteritems(), key=operator.itemgetter(1))[0]
Out[5]: 'jan'
Community
  • 1
  • 1
ninMonkey
  • 7,211
  • 8
  • 37
  • 66
0

If dictionaries can't be used you could implement a similar logic with a different data representation for the elements in the list. You could put it in the format "Jan|10.0" where the first part references the month and the second the rainfall value. The code in that case would be something like this:

year=[]
months = ["Jan","Feb","March","April","May","June","July","August","September","October","November","December"]

for i in months:
    year.append(i+"|"+str(input('Please enter '+i+' rainfall: ')))

key_func = lambda s:s.split("|")[1]
split_func = lambda s,index:s.split("|")[index]

min_rainfall = min(year,key=key_func)
print("Minimum Rainfall: "+split_func(min_rainfall,1)+" in the month of "+split_func(min_rainfall,0))

max_rainfall = max(year,key=key_func)
print("Max Rainfall: "+split_func(max_rainfall,1)+" in the month of "+split_func(max_rainfall,0))

total = sum([float(split_func(p,1)) for p in year])
print("Total Rainfall: "+str(total))

print("Average Rainfall: "+str(float(total/len(year))))
Rohit Jose
  • 188
  • 3
  • 13
0

This is how i did it

daily_rainfall = [
    .23,  # Monday
    2.12,  # Tuesday
    .50,  # Wednesday
    .79,  # Thursday
    2.00,  # Friday
    .99,  # Saturday
    1.75   # Sunday
]

total = 0
for day in daily_rainfall:
    total += day

average = total / len(daily_rainfall)

print(f'Weekly rain amount: {total:.2f} inches')
print(f'Daily average rain amount: {average:.2f} inches')
J. Murray
  • 1,460
  • 11
  • 19