Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2012, 12:48 AM
q81101 q81101 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 3 q81101 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old September 6th, 2012, 02:59 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 412 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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)

Reply With Quote
  #3  
Old September 6th, 2012, 02:53 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > I didn't know where did it goes wrong with my code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap