for loop

S

Shawn Minisall

#Intro
print "*********************************************"
print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
print "*********************************************"

print "This program will predict the size of a population of organisms."
print
print
organisms=input("Please enter the starting number of organisms: ")

increase=input("Please enter the average daily population increase
as a percentage (20% = .20): ")

days=input("Please enter the number of days that they will multiply: ")

print " Day Population"
print "----------------------------------------------------------"


for p in range (days):

population = organisms * population * increase

print days,

print "\t\t\t\t",population

I'm having problems with my for loop here to calculate estimated
population output to a table. Instead of knowing how much I want to
loop it, the loop index is going to be whatever number of days the user
enters. When I run my program, it asks the 3 questions above but then
just dead stops at a prompt which leads me to believe there's something
wrong with my loop. I have the exact same setup in another program, but
the loop index has a specific value. I tried (0,days), (1,days) ect.
and I don't think for loops need accumulators, I've tried it with one
anyways and it still stops.

Any idea's?

thx
 
M

Marc 'BlackJack' Rintsch

#Intro
print "*********************************************"
print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
print "*********************************************"

print "This program will predict the size of a population of organisms."
print
print
organisms=input("Please enter the starting number of organisms: ")

increase=input("Please enter the average daily population increase
as a percentage (20% = .20): ")

days=input("Please enter the number of days that they will multiply: ")

print " Day Population"
print "----------------------------------------------------------"


for p in range (days):

population = organisms * population * increase

print days,

print "\t\t\t\t",population

I'm having problems with my for loop here to calculate estimated
population output to a table. Instead of knowing how much I want to
loop it, the loop index is going to be whatever number of days the user
enters. When I run my program, it asks the 3 questions above but then
just dead stops at a prompt which leads me to believe there's something
wrong with my loop.

It should not run at all as it is indented inconsistently. If that
problem is corrected it will stop with a `NameError` because you try to
read `population` before anything was assigned to it.

Ciao,
Marc 'BlackJack' Rintsch
 
M

mensanator

It should not run at all as it is indented inconsistently. If that
problem is corrected it will stop with a `NameError` because you try to
read `population` before anything was assigned to it.

Ciao,
Marc 'BlackJack' Rintsch

Also, I would guess that you want to print p, not days.
 
S

Steven D'Aprano

I'm having problems with my for loop here to calculate estimated
population output to a table.

The code you have posted has inconsistent indentation. It won't run as
you supply it.

Instead of knowing how much I want to
loop it, the loop index is going to be whatever number of days the user
enters.

Don't use input(), it doesn't do what you think it does.

Use int(raw_input()) instead.


When I run my program, it asks the 3 questions above but then
just dead stops at a prompt which leads me to believe there's something
wrong with my loop.

Correcting the indentation in your code shows that it doesn't "just dead
stops at a prompt". So either you've posted the wrong code, or you aren't
telling us something. Is it possible that your program is raising an
exception telling you that something is wrong?


I have the exact same setup in another program, but
the loop index has a specific value.

Loop indexes always have a specific value. I think what you mean is that
it has a fixed, hard-coded value e.g. range(10) instead of range(x).

I tried (0,days), (1,days) ect.

Ah, the time-honored technique of "debugging by making random changes".

range(days) is the correct thing to use, provided days is a positive
whole number greater than zero.

and I don't think for loops need accumulators, I've tried it with one
anyways and it still stops.

I thought you said the problem was that the loop didn't run at all? The
loop is supposed to stop.

Any idea's?

Yes.

Your code has at least one bug in it. When you run that code, Python
prints an exception telling you what is wrong. That is useful
information, don't just ignore it.
 
M

mensanator

Also, I would guess that you want to print p, not days

Oh, and your calculation is incorrect. You don't multiply by
organisms in every loop iteration, organisms is the initial
value of population, so you can solve the "Name" error by doing
population = organisms before the for..loop.

And since you're asking for an increase, you don't multiply by the
percent (as that would decrease the population), but instead by
1+increase.

Also, does day==0 represent the first day of increase or the
initial value? One would normally expect day==0 to be the initial
value, but as written, day==0 is the first day of increase. I
would use xrange(1,days+1) instead.

Lastly, you can't have a fraction of an organism, right? You might
want to print your floating point population rounded to an integer.

population = organisms
for p in xrange(1,days+1):
population = population * (1 + increase)
print p,
print "\t\t\t\t%0.0f" % (population)

## *********************************************
## WELCOME TO THE POPULATION GROWTH CALCULATOR
## *********************************************
## This program will predict the size of a population of organisms.
##
##
## Please enter the starting number of organisms: 100
## Please enter the average daily population increase as a percentage
(20% = .20): 0.25
## Please enter the number of days that they will multiply: 8
## Day Population
## ----------------------------------------------------------
## 1 125
## 2 156
## 3 195
## 4 244
## 5 305
## 6 381
## 7 477
## 8 596
 
S

Shawn Minisall

Thanks, everyone! Using everyone's suggestions and points, the program
is working great now. Here's the updated code.

:)

import math

def main():
#Declare and initialize variables
#starting number of organisms
organisms = 0
#average daily population increase as %
increase = 0.0
#number of days they will multiply
days = 0
#population prediction
population = 0.0

#Intro
print "*********************************************"
print "WELCOME TO THE POPULATION GROWTH CALCULATOR"
print "*********************************************"

print "This program will predict the size of a population of organisms."
print
print
while organisms <=1:
organisms=input("Please enter the starting number of organisms: ")
if organisms <=1:
print "Error. Population must be at least two."

while increase <=0:
increase=input("Please enter the average daily population
increase as a percentage (20% = .20): ")
if increase <=0:
print "The percent of increase must be positive."

while days <=0:
days=input("Please enter the number of days that they will
multiply: ")
if days <=0:
print "The number of days must be positive."

print " Day Population"
print "----------------------------------------------------------"
population = organisms


for p in range (1,days+1):
if( p > 1 ):
population = population + ( population * increase )


print "\t",p,
 
M

mensanator

Thanks, everyone! Using everyone's suggestions and points, the program
is working great now.

Actually, it's not. I assume not printing the population
was a copy error.

But, by adding the "if (p>1):", you have now made
day 1 the initial population, so if you ask for
8 days of multiplication, you actually only get 7.

Although your code is working, it's not giving you
the correct answer. Most of the time in these kinds
of problems, days means elapsed days. That means for
100 organisms @ 25% growth/day, there will be 125
after 1 elapsed day. But that "if (p>1):" means you
show 100 at day 1, which is wrong. You have 100 after
0 elapsed days (or day 0).
 
S

Shawn Minisall

I agree, but if I want to get a A on the program, thats how my professor
wants the output.

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top