help with python code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imran akhtar
    New Member
    • Nov 2008
    • 64

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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*(bir th_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.

    Code:
    def projected_pop(year):
        ....code....
        return calculated_pop
    The input could be done something like this:
    Code:
    print int(projected_pop(int(raw_input("Enter year"))))

    Comment

    • Smygis
      New Member
      • Jun 2007
      • 126

      #3
      its a simple formula.

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

      If i remember my math correctly. Something i'm not sure of.

      Comment

      • Kvod10
        New Member
        • Nov 2008
        • 7

        #4
        try using this to start

        Code:
        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_i nput("whatever you wanna say"))) for the input. I wouldn't just use int; your dealing with a lot of decimals

        Comment

        • Kvod10
          New Member
          • Nov 2008
          • 7

          #5
          here's the function a little clearer
          Code:
          float(int(raw_input(" Prompt ")))

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by Kvod10
            here's the function a little clearer
            Code:
            float(int(raw_input(" Prompt ")))
            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.

            Comment

            • imran akhtar
              New Member
              • Nov 2008
              • 64

              #7
              birthrate code

              below i have made the code, but comes up with error
              message: TypeError: unsupported operand type(s) for ^: 'int' and 'float'
              Code:
              (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)
              Last edited by bvdet; Dec 16 '08, 03:19 PM. Reason: Add code tags

              Comment

              • bvdet
                Recognized Expert Specialist
                • Oct 2006
                • 2851

                #8
                Character "^" is the Python bitwise XOR operator symmetric difference operator for sets. I think you need the power operator "**".

                Comment

                • imran akhtar
                  New Member
                  • Nov 2008
                  • 64

                  #9
                  how would i power the operrator , is possiable if you could show
                  thanks

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    The power operator works like this:
                    Code:
                    >>> 2**2
                    4
                    >>> 2**0.5
                    1.4142135623730951
                    >>>

                    Comment

                    • imran akhtar
                      New Member
                      • Nov 2008
                      • 64

                      #11
                      population

                      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=(Basepo p + (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

                      Comment

                      • bvdet
                        Recognized Expert Specialist
                        • Oct 2006
                        • 2851

                        #12
                        Replace "^" with "**":
                        Code:
                        year**(Birthrate - Deathrate)
                        -BV

                        Comment

                        • imran akhtar
                          New Member
                          • Nov 2008
                          • 64

                          #13
                          thanks it works, very greatful

                          Comment

                          Working...