Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,837 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,579 people online right now. Registration is fast and FREE... Join Now!




Python rainfall

 
Reply to this topicStart new topic

Python rainfall, I need help

kempablavitt
28 Sep, 2008 - 04:18 AM
Post #1

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 2


My Contributions
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?


CODE

# -*- 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
        return total

    #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()




User is offlineProfile CardPM
+Quote Post

Stutzbach
RE: Python Rainfall
28 Sep, 2008 - 06:01 AM
Post #2

New D.I.C Head
*

Joined: 6 Jul, 2008
Posts: 10



Thanked: 3 times
My Contributions
QUOTE(kempablavitt @ 28 Sep, 2008 - 06:18 AM) *

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:

CODE

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.
User is offlineProfile CardPM
+Quote Post

kempablavitt
RE: Python Rainfall
28 Sep, 2008 - 08:00 AM
Post #3

New D.I.C Head
*

Joined: 28 Sep, 2008
Posts: 2


My Contributions
QUOTE(Stutzbach @ 28 Sep, 2008 - 07:01 AM) *

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. sad.gif
User is offlineProfile CardPM
+Quote Post

David W
RE: Python Rainfall
7 Oct, 2008 - 09:58 PM
Post #4

D.I.C Regular
Group Icon

Joined: 20 Sep, 2008
Posts: 315



Thanked: 16 times
Dream Kudos: 275
My Contributions
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. sad.gif



you could try something like this ... smile.gif

CODE
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
        return total

    #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()

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 05:12PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month