473,387 Members | 1,590 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

help with python code

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 2121
bvdet
2,851 Expert Mod 2GB
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
126 100+
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
Kvod10
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
Kvod10
7
here's the function a little clearer
Expand|Select|Wrap|Line Numbers
  1. float(int(raw_input(" Prompt ")))
Nov 30 '08 #5
bvdet
2,851 Expert Mod 2GB
@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
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
2,851 Expert Mod 2GB
Character "^" is the Python bitwise XOR operator symmetric difference operator for sets. I think you need the power operator "**".
Dec 16 '08 #8
how would i power the operrator , is possiable if you could show
thanks
Dec 16 '08 #9
bvdet
2,851 Expert Mod 2GB
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
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, 439 views)
Dec 17 '08 #11
bvdet
2,851 Expert Mod 2GB
Replace "^" with "**":
Expand|Select|Wrap|Line Numbers
  1. year**(Birthrate - Deathrate)
-BV
Dec 17 '08 #12
thanks it works, very greatful
Dec 17 '08 #13

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

Similar topics

3
by: danissim | last post by:
help!! hi people could you to tell me how can i execute a program after i wrote the code??? thanks!
5
by: duikboot | last post by:
Hi all, I'm trying to export a view tables from a Oracle database to a Mysql database. I create insert statements (they look alright), but it all goes wrong when I try to execute them in Mysql,...
0
by: python-help-bounces | last post by:
Your message for python-help@python.org, the Python programming language assistance line, has been received and is being delivered. This automated response is sent to those of you new to...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
3
by: zxo102 | last post by:
Hi there, I have a python application (many python scripts) and I start the application like this python myServer.py start in window. It is running in dos window. Now I would like to put it...
1
by: Paul Rubin | last post by:
In Windows if you click the Help dropdown, IDLE launches a help window as it should. The help contents are included in the installation. In Linux, clicking Help launches a web browser, which is...
12
by: BillJosephson | last post by:
Hi all. I am trying to use jEdit, but can't seem to download any plugins. I go to Plugin Manager, and select Download options. I update the mirro and can see lots of servers. I select the first...
12
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
2
by: astrogirl77 | last post by:
Hi, I'm new to Python and am hoping to find help with coding a Python script, applet. I code in an old version of Visual Basic 4.0, I have a simple app that is about 3 and a half pages of code...
0
by: Ahmed, Shakir | last post by:
Thanks everyone who tried to help me to parse incoming email from an exchange server: Now, I am getting following error; I am not sure where I am doing wrong. I appreciate any help how to resolve...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.