Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
September 6th, 2012, 12:48 AM
 q81101
Registered User

Join Date: Sep 2012
Posts: 3
Time spent in forums: 45 m 6 sec
Reputation Power: 0
I didn't know where did it goes wrong with my code

Code:
# File: PopulationProjection.py

# Assign value for birth, death, and immigrant

birth= 7
death= 13
immigrant= 45

# Compute for population next 5 years

firstyear = 312032486 + ((365*24*60*60) // birth) - ((365*24*60*60) // death) + ((365*24*60*60) // immigrant)

secondyear = 312032486 + (2*((365*24*60*60) // birth)) - (2*((365*24*60*60) // death)) + (2*((365*24*60*60)// immigrant))

thirdyear = 312032486 + (3*((365*24*60*60) // birth)) - (3*((365*24*60*60) // death)) + (3*((365*24*60*60) // immigrant))

fourthyear = 312032486 + (4*((365*24*60*60) // birth)) - (4*((365*24*60*60) // death)) + (4*((365*24*60*60) // immigrant))

fifthyear = 312032486 + (5*((365*24*60*60) // birth)) - (5*((365*24*60*60) // death)) + (5*((365*24*60*60) // immigrant))

# Display results

print("first year=", firstyear)
print("second years=", secondyear)
print("third years=", thirdyear)
print("fourth years=", fourthyear)
print("fifth years=", fifthyear)

I need to find the population number in next five years so

For my calculation: Current population + birth - death + immigrant
for next year, I just mutiply the (birth, death, and immigrant number)

I convert year to "second": 365*24*60*60
The assign values are in second
312032486 is the current population

Also, how can I deal with the remainder?(ex: 5 // 2 = 2 but actually is 2.5)
I know there is the remainder
but I don't know what to write in the code.

#2
September 6th, 2012, 02:59 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412
Time spent in forums: 1 Week 7 h 14 m 58 sec
Reputation Power: 65
Quote:
 Originally Posted by q81101 Also, how can I deal with the remainder?(ex: 5 // 2 = 2 but actually is 2.5) I know there is the remainder but I don't know what to write in the code.

“%” is the remainder operator (5 % 2 = 1).

You might try changing the population over time instead of always starting with the initial value, as in the following Idle session. (Of course I have no idea if these are the values you like but I tried to do the same calculations as you. Also notice how I tried to avoid repeating code, since that’s bad practice. Repeating code means more places to enter mistakes.)

Code:
>>> population = 312032486
>>> year_in_secs = 365 * 24 * 60 * 60
>>> birth = year_in_secs // 7
>>> death = year_in_secs // 13
>>> immigration = year_in_secs // 45
>>> for year in range(5):
population += (birth - death + immigration)
print('Year {0}, population {1}'.format(year + 1, population))

Year 1, population 314812582
Year 2, population 317592678
Year 3, population 320372774
Year 4, population 323152870
Year 5, population 325932966
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

#3
September 6th, 2012, 02:53 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 3,458
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
I'd compute this exponentially. Maybe that's a little silly with immigration.
Code:
import math
P0 = 312032486.0
birth= 7 # seconds per person
death= 13 # seconds per person
immigrant= 45 # seconds per person
spd = 24*60*6
dpy = 365.242

people_per_day = spd/birth + spd/immigrant - spd/death
print(people_per_day)
rate = people_per_day/P0

print('rate of change: %g'%rate)
# Compute for population next 5 years

print('years from now    population (thousands)')
for year in range(6):
P = P0*math.exp(rate*year*dpy)
print('%12d      %-12d'%(year,int(0.5+P/1e3)))
This method brings an additional 4000 people.
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > I didn't know where did it goes wrong with my code