The function has to show the population for year 1, 2 and so on until the population reaches 1 million. The population increases by 8% every year?

def population():
    current=input("please enter the current population of the city: ")
increase=current/100*8+current
year 1=increase

while increase>1000000?

Recommended Answers

All 10 Replies

def population_growth(argBase, argMax, argRate):
    assert argBase < argMax
    assert argRate > 0
    theAnswer = [argBase]
    iterAnswer = argBase
    while True:
        iterAnswer *= 1+argRate
        theAnswer.append(iterAnswer)
        if iterAnswer > argMax:
            break
    return theAnswer
uBasePop = int(raw_input("Enter the base population: "))
print population_growth(uBasePop, 1000000, .08)

I didn't know what you're needs were so I made it fairly abstract. Is this what you're looking for?

def population_growth(argBase, argMax, argRate):
    assert argBase < argMax
    assert argRate > 0
    theAnswer = [argBase]
    iterAnswer = argBase
    while True:
        iterAnswer *= 1+argRate
        theAnswer.append(iterAnswer)
        if iterAnswer > argMax:
            break
    return theAnswer
uBasePop = int(raw_input("Enter the base population: "))
print population_growth(uBasePop, 1000000, .08)

I didn't know what you're needs were so I made it fairly abstract. Is this what you're looking for?

yeah i havnt learned any of the operations you have put on here such as argbase, append etc... i need to use a while loop and display the results till it reaches 1million..... thx for the help

here is my current code.. but it seems to work fine for the first year.. but the second year i want it increase 8% from the 1st year.. hw could i do tht?

def population():
    current=input("please enter the current population of the city: ")
    increase=current/100*8+current
    i=0
    while i<1000000:
        i=i+increase
        print "The population is", i

You have to put your increase calculation in the loop.

def population():
    current=input("please enter the current population of the city: ")
    yearCount = 1
    print "At year 0 The population is ",current
    while current < 1000000:
        current += current*0.08
        print "At year ",yearCount,"The population is ",current
        yearCount += 1
if __name__ == '__main__':
    population()

yearCount += 1
if name == 'main':
population()

can you just tell me about what is happening in these lines?

It is just a

def main():
    main()

for testing the function population. Then all I do is call population(). If those 2 lines of code where not present then the function population would never run.
Also the yearCount is just totaling up the years.
yearCount += 1 is the same as yearCount = yearCount + 1

Member Avatar for masterofpuppets

here's a more general version of this :) :

def population( start, maxPop, rate ):
    start += ( ( float( rate ) / 100 ) * start )
    year = 1
    while start <= maxPop:
        print year, "-", int( start )
        start += ( ( float( rate ) / 100 ) * start )
        year += 1

startPop = input( "Enter base population: " )
lim = input( "Enter population limit: " )
rate = input( "Enter rate of change in %, ( e.g. 8 ): " )

population( startPop, lim, rate )

It is just a

def main():
    main()

for testing the function population. Then all I do is call population(). If those 2 lines of code where not present then the function population would never run.
Also the yearCount is just totaling up the years.
yearCount += 1 is the same as yearCount = yearCount + 1

if __name__ == '__main__':, is this the sam as saying def main()? and could you please tell me what you mean by totaling up the years?.. i understand the current+=current*0.08, this means that it does the interest on the predecessor and not the actual value which we have put in right?

Member Avatar for leegeorg07

surely you dont need all the complication?

for the basic, and original problem surely you could use:

basepop=30 #or whatever you want.
while basepop<1000000:
  print basepop
  basepop*=1.08

for masterofpuppet's version I would use:

def population( start, maxPop, rate ):
    year = 1
    rate=1+rate
    while start < maxPop:
        print year, "-", int( start )
        start *=rate
        year += 1

startPop = input( "Enter base population: " )
lim = input( "Enter population limit: " )
rate = input( "Enter rate of change in %, ( e.g. 8 ): " )

population( startPop, lim, rate )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.