2 Replies - 1016 Views - Last Post: 01 February 2010 - 06:24 PM Rate Topic: -----

#1 Perl and Python  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 01-February 10

Missing a loop somewhere.

Post icon  Posted 01 February 2010 - 01:30 PM

Hey, I am missing a loop here somewhere but am not sure where. What I have now is that it just prints one value instead of a value for every year. I tried a "for" loop but that was unsuccessful. Any help would be appreciated.

Here is my code:
 

def predict_population():

	import math

	sp = input("What is the starting population? ")
	r = input("What is the rate of population growth (between 0 and 1)? ")

	while r > 1 or r < 0:
		print "Invalid population growth rate."
		r = input("Please enter a valid rate: ")

	t = input("How many years to predict? ")
	tp = sp * math.exp(r*t)
	print "The population after",t,"years will be",tp

predict_population()




This is the result that I am aiming for:
Enter starting population: 100
Enter growth rate (a value between 0 and 1): 0.3
Enter number of years to predict: 10
Population after 0 years will be 100
Population after 1 years will be 134
Population after 2 years will be 182
Population after 3 years will be 245
Population after 4 years will be 332
Population after 5 years will be 448
Population after 6 years will be 604


This post has been edited by Perl and Python: 01 February 2010 - 01:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Missing a loop somewhere.

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 756
  • View blog
  • Posts: 1,990
  • Joined: 23-December 08

Re: Missing a loop somewhere.

Posted 01 February 2010 - 01:47 PM

View PostPerl and Python, on 1 Feb, 2010 - 12:30 PM, said:

Hey, I am missing a loop here somewhere but am not sure where. What I have now is that it just prints one value instead of a value for every year. I tried a "for" loop but that was unsuccessful. Any help would be appreciated.

Here is my code:
 

def predict_population():

	import math

	sp = input("What is the starting population? ")
	r = input("What is the rate of population growth (between 0 and 1)? ")

	while r > 1 or r < 0:
		print "Invalid population growth rate."
		r = input("Please enter a valid rate: ")

	t = input("How many years to predict? ")
	tp = sp * math.exp(r*t)
	print "The population after",t,"years will be",tp

predict_population()




This is the result that I am aiming for:
Enter starting population: 100
Enter growth rate (a value between 0 and 1): 0.3
Enter number of years to predict: 10
Population after 0 years will be 100
Population after 1 years will be 134
Population after 2 years will be 182
Population after 3 years will be 245
Population after 4 years will be 332
Population after 5 years will be 448
Population after 6 years will be 604




You've already done most of the work boss. Here's what you need to change:


def predict_population():

	import math

	sp = input("What is the starting population? ")
	r = input("What is the rate of population growth (between 0 and 1)? ")

	while r > 1 or r < 0:
		print "Invalid population growth rate."
		r = input("Please enter a valid rate: ")

	t = input("How many years to predict? ")

		for i in range(t):
			tp = sp * math.exp(r*i)
			print "The population after",i,"years will be",tp

predict_population()




El done.
-Adam
http://www.learningp...rogramming.com/

This post has been edited by atraub: 01 February 2010 - 01:48 PM

Was This Post Helpful? 1
  • +
  • -

#3 Perl and Python  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 01-February 10

Re: Missing a loop somewhere.

Posted 01 February 2010 - 06:24 PM

Thanks for the reply, it was very informative.

I was having problems because originally I had:
for i in range(t):
			tp = sp * math.exp(r*t)
			print "The population after",t,"years will be",tp


But when I changed it to:
for i in range(t):
			tp = sp * math.exp(r*i)
			print "The population after",i,"years will be",tp


it worked. :D

This post has been edited by Perl and Python: 01 February 2010 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1