By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,551 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

help with python code

P: 64
A borough has made the following prognosis for the changes in population over the next few years:
• At the start of 2004 there were 26 000 inhabitants
• The rates of births and deaths are estimated at 0.7% and 0.6% of the population respectively
• The number of people moving in and out of the borough annually is estimated at 300 and 325 respectively.

i have to write a program that calculate the borough’s estimated population at the beginning of a particular year. The year in question should be requested from the user.

i have had, go, but does not seem to work. i would like help on how to start the code.
Nov 27 '08 #1
Share this Question
Share on Google+
12 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Show us the work you have done so far, and we will try to help.

I think the calculation will go something like this:

population += population*(birth_rate - death_rate)
population += (movein_rate - moveout_rate)

This calculation will need to be done for each year in the difference between the target date and 2004.

I would create a function that returns the projected population.

Expand|Select|Wrap|Line Numbers
  1. def projected_pop(year):
  2.     ....code....
  3.     return calculated_pop
The input could be done something like this:
Expand|Select|Wrap|Line Numbers
  1. print int(projected_pop(int(raw_input("Enter year"))))
Nov 28 '08 #2

Smygis
100+
P: 126
its a simple formula.

(BasePopulation + (MoveinRate - MoveoutRate) * year) * year^(BirthRate - DeathRate)

If i remember my math correctly. Something i'm not sure of.
Nov 28 '08 #3

P: 7
try using this to start

Expand|Select|Wrap|Line Numbers
  1. def popfunct(year):
  2.     baseyear = 2004
  3.     basepop = 26000
  4.     birthrate = 0.007
  5.     deathrate = 0.006
  6.     movein = 300
  7.     moveout = 325
  8.  
  9.     newpop = #...math...
  10.  
  11.     return newpop
when you make your GUI use float(int(raw_input("whatever you wanna say"))) for the input. I wouldn't just use int; your dealing with a lot of decimals
Nov 30 '08 #4

P: 7
here's the function a little clearer
Expand|Select|Wrap|Line Numbers
  1. float(int(raw_input(" Prompt ")))
Nov 30 '08 #5

bvdet
Expert Mod 2.5K+
P: 2,851
@Kvod10
Thanks for posting Kvod10.

Actually, there is no need to convert to float in this case. An integer multiplied or divided by a float will return a float.

I would recommend type casting the appropriate data type. The input is supposed to be a year, therefore the appropriate data type is integer. A population count should be integer also. Since the rates are float, the interim calculation will produce a float which is necessary. The future population should therefore be cast to integer.
Nov 30 '08 #6

P: 64
below i have made the code, but comes up with error
message: TypeError: unsupported operand type(s) for ^: 'int' and 'float'
Expand|Select|Wrap|Line Numbers
  1. (result=(Basepop + (Movein - Moveout) * year) * year^(Birthrate - Deathrate) 
  2. year=input("enter year:")
  3.  
  4. Birthrate = 0.007
  5. Deathrate = 0.006
  6. Movein = 300
  7. Moveout = 325
  8. Baseyear = 2004
  9. Basepop=26000
  10.  
  11.  
  12. result=(Basepop + (Movein - Moveout) * year) * year^(Birthrate - Deathrate)
  13.  
  14. print "estimated population", (result)
Dec 16 '08 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Character "^" is the Python bitwise XOR operator symmetric difference operator for sets. I think you need the power operator "**".
Dec 16 '08 #8

P: 64
how would i power the operrator , is possiable if you could show
thanks
Dec 16 '08 #9

bvdet
Expert Mod 2.5K+
P: 2,851
The power operator works like this:
Expand|Select|Wrap|Line Numbers
  1. >>> 2**2
  2. 4
  3. >>> 2**0.5
  4. 1.4142135623730951
  5. >>> 
Dec 16 '08 #10

P: 64
sorry, i am confused with power operater, and i have now messed up my code lol, before it worked, after i made changes, it does work, and displays an arror messge such as

(result=(Basepop + (Movein - Moveout) * year) * year^(Birthrate - Deathrate)
TypeError: unsupported operand type(s) for ^: 'int' and 'float'

i have attched the code in txt file, so you can see problem.
Attached Files
File Type: txt pop.txt (804 Bytes, 388 views)
Dec 17 '08 #11

bvdet
Expert Mod 2.5K+
P: 2,851
Replace "^" with "**":
Expand|Select|Wrap|Line Numbers
  1. year**(Birthrate - Deathrate)
-BV
Dec 17 '08 #12

P: 64
thanks it works, very greatful
Dec 17 '08 #13

Post your reply

Sign in to post your reply or Sign up for a free account.