Python rainfall

Page 1 of 1

3 Replies - 15214 Views - Last Post: 07 October 2008 - 10:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=65351&amp;s=618ee868ae5c94680a4f35a537cfdfd6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kempablavitt

Reputation: 0
• Posts: 2
• Joined: 28-September 08

Python rainfall

Posted 28 September 2008 - 05:18 AM

I need to make a program in python that ask the user for the amount of rain each month and calculates the total and avarage of rain. And then I need to show the months that it rained the most and the same with less.
I have writen this but I don't know how to get the names of the months, it works if the months got a number that represents it in the list. I need help with the last thing. How do I change so it shows the name of the months instead?

```# -*- coding: cp1252 -*-

def main():

num_months = 12

#Definera lite listor
months = [0] * num_months
name_months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec']

#Funktion för räkna det totala från listan
def total(months):
total = 0

for num in months:
total += num

#Få alla nummer från användaren
for index in range(num_months):
print 'Enter the amount of rain in',
months[index] = input(str(index + 1) + ': ')
print 'The total is', total(months), 'mm.'

#Genomsnittet
avarage = float(total(months)) / float(num_months)
print 'The avarage rainfall is', avarage, 'mm.'

#Översätt från siffror till månadernas namn

#Sortera listan
months.sort()

print 'Lowest is', months[0]

print 'Most is', months[11]

main()

```

Is This A Good Question/Topic? 0

Replies To: Python rainfall

#2 Stutzbach

Reputation: 6
• Posts: 29
• Joined: 06-July 08

Re: Python rainfall

Posted 28 September 2008 - 07:01 AM

kempablavitt, on 28 Sep, 2008 - 06:18 AM, said:

I have writen this but I don't know how to get the names of the months, it works if the months got a number that represents it in the list. I need help with the last thing. How do I change so it shows the name of the months instead?

You could change "months" the be a list of (amount of rainfall, name of month) tuples. Something like this:

```months = []
for index in range(num_months):
print 'Enter the amount of rain in',
months[index] = (input(str(index + 1) + ': '), name_months[index])

```

That way, when you sort the list, the names of the months stay with the amount of rainfall.

#3 kempablavitt

Reputation: 0
• Posts: 2
• Joined: 28-September 08

Re: Python rainfall

Posted 28 September 2008 - 09:00 AM

Stutzbach, on 28 Sep, 2008 - 07:01 AM, said:

You could change "months" the be a list of (amount of rainfall, name of month) tuples.

That way, when you sort the list, the names of the months stay with the amount of rainfall.

I see what you mean but I can't get it to work...its a good idea though. I have tried many things now but still can't get it to work.

#4 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

Re: Python rainfall

Posted 07 October 2008 - 10:58 PM

Quote

I see what you mean but I can't get it to work...its a good idea though. I have tried many things now but still can't get it to work.

you could try something like this ...

```def main():

#Definera lite listor
months = [0] * 12
name_months = ['Jan','Feb','Mar','Apr','May','Jun', \
'Jul','Aug','Sep','Oct','Nov','Dec']

#Funktion för räkna det totala från listan
def total(months):
total = 0
for num in months:
total += num

#Få alla nummer från användaren
for index in range(12):
print 'Enter the amount of rain in',
months[index] = input(name_months[index] + ': ')
print 'The total is', total(months), 'mm.'

#Genomsnittet
avarage = total(months) / 12.0
print 'The avarage rainfall is', avarage, 'mm.'

#Översätt från siffror till månadernas namn

# firstly ... make an unsorted copy ...
m_copy = months[0:]
# now ...
#Sortera listan
months.sort()

lowest = months[0]
print 'Lowest is', lowest, 'in',
# we are going to make a list of the month names that = lowset
lows = []
for i in range (12):
if m_copy[i] == lowest:
lows.append( name_months[i] )
for i in range (len(lows)):
print lows[i],
if i < len(lows)-1: print 'and',
print

highest = months[11]
print 'Highest is', highest, 'in',
# we are going to make a list of the month names that = highest
highs = []
for i in range (12):
if m_copy[i] == highest:
highs.append( name_months[i] )
for i in range (len(highs)):
print highs[i],
if i < len(highs)-1: print 'and',

print

main()
```