# Missing a loop somewhere.

Page 1 of 1

## 2 Replies - 1591 Views - Last Post: 01 February 2010 - 06:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=153308&amp;s=fb2db0fd87567d5c9ea07443ab0461de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Perl and Python

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

# Missing a loop somewhere.

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

• Pythoneer

Reputation: 825
• Posts: 2,208
• Joined: 23-December 08

## Re: Missing a loop somewhere.

Posted 01 February 2010 - 01:47 PM

Perl 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.
http://www.learningp...rogramming.com/

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

### #3 Perl and Python

Reputation: 1
• 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.

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