for loop
Shawn Minisall
trekker182 at comcast.net
Mon Oct 22 22:12:40 EDT 2007
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,
mensanator at aol.com wrote:
> On Oct 22, 5:37 pm, "mensana... at aol.com" <mensana... at aol.com> wrote:
>
>> On Oct 22, 5:22 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
>>
>>
>>
>>
>>
>>
>>> On Mon, 22 Oct 2007 18:17:56 -0400, Shawn Minisall wrote:
>>>
>>>> #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
>>>
>> 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
>
>
>
More information about the Python-list
mailing list