3 Replies - 4003 Views - Last Post: 07 October 2008 - 10:58 PM Rate Topic: -----

#1 kempablavitt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
		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()




Is This A Good Question/Topic? 0
  • +

Replies To: Python rainfall

#2 Stutzbach  Icon User is offline

  • New D.I.C Head

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

Re: Python rainfall

Posted 28 September 2008 - 07:01 AM

View Postkempablavitt, 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.
Was This Post Helpful? 0
  • +
  • -

#3 kempablavitt  Icon User is offline

  • New D.I.C Head

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

Re: Python rainfall

Posted 28 September 2008 - 09:00 AM

View PostStutzbach, 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. :(
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • 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
		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()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1