### Thread: I didn't know where did it goes wrong with my code

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2012
Posts
3
Rep 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. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
436
Rep Power
67
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```
3. 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.