439,986 Members | 1,551 Online
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
12 Replies

 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 def projected_pop(year):     ....code....     return calculated_pop The input could be done something like this: Expand|Select|Wrap|Line Numbers print int(projected_pop(int(raw_input("Enter year")))) Nov 28 '08 #2

 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 def popfunct(year):     baseyear = 2004     basepop = 26000     birthrate = 0.007     deathrate = 0.006     movein = 300     moveout = 325       newpop = #...math...       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 float(int(raw_input(" Prompt "))) Nov 30 '08 #5

 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 (result=(Basepop + (Movein - Moveout) * year) * year^(Birthrate - Deathrate)  year=input("enter year:")   Birthrate = 0.007 Deathrate = 0.006 Movein = 300 Moveout = 325 Baseyear = 2004 Basepop=26000     result=(Basepop + (Movein - Moveout) * year) * year^(Birthrate - Deathrate)   print "estimated population", (result) Dec 16 '08 #7

 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

 Expert Mod 2.5K+ P: 2,851 The power operator works like this: Expand|Select|Wrap|Line Numbers >>> 2**2 4 >>> 2**0.5 1.4142135623730951 >>>  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
 pop.txt (804 Bytes, 388 views)
Dec 17 '08 #11

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

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