How to create a pension calculator?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Johan Ericsson

    How to create a pension calculator?

    Hi guys,
    Im using windows 7 and python 2.7.

    I need to read in info from a file in the format:
    name/age/amount
    for example: johan/20/1000

    Then for example if you have 1000$ and the growth 5% for 20 years it would be 1000*1.05^20.

    The only thing that isnt read from the file is the interest which is read from a raw_input.

    This is the code I have so far and im kind of stuck now on how to get it working. I am not very good with classes and objects...

    Code:
    import math
    
    class Pension:
        
        def __init__(self, name, age, amount):
            self.name = name
            self.age = age
            self.amount = amount
            
    
    def percentage():
        p = float(raw_input('How many percent is the growth? '))
        percentage = (p/100)+1
        return percentage
    
    
    def read():
        f = open('file.txt','r')
        rad = f.readline()
        info = list()
        while rad != '':
            rad = rad.rstrip('\n')
            parts = rad.split('/')
            name = str(parts[0])
            age = int(parts[1])
            amount = float(parts[2])
            temp = Pension(name,age,amount)
            info.append(temp)
            rad = f.readline()
        f.close()
        return info
    
    
    def printing(info):
        for i in range(len(info)):
            print info[i]
    
    
    
    percentage()
    info = read()
    printing(info)
    I would love it if anyone of you could help me out a bit :)
    Thanks a lot in advance.
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    This is the code I have so far and im kind of stuck now on how to get it working.

    I would love it if anyone of you could help me out a bit
    What does this mean? One obvious flaw is that "info" is a list of classes so you would print the fields of the class. An example follows. See "Defining Classes" here for basic class info. Note that you are doing the same thing as is done in "Using Classes" except you are doing this within a loop.
    Code:
    def printing(info):
        for class_instance in info:
            print class_instance.name, class_instance.age, \
                  class_instance.amount
    ##
    ##   you could also add a print method to the class 
    ##
    class Pension:
     
        def __init__(self, name, age, amount):
            self.name = name
            self.age = age
            self.amount = amount
    
        def print_data(self):
            print self.name, self.age, self.amount
    
    def printing(info):
        for class_instance in info:
            class_instance.print_data()

    Comment

    • Johan Ericsson

      #3
      That's great!
      Thank you very very much. :D

      Now the only thing I have left is adding the formula with the percentage and then print the finished list, which should include the name and how much they would have once they reach the age of 65.

      Comment

      • Johan Ericsson

        #4
        Is there a way to use the sum() function in order to calculate this?

        Or is there an integral function already in python or possible in the math module?

        SUM OF[ amount*math.pow (percentage, yearsLeft-1) + amount*math.pow (percentage, yearsLeft-2) + amount*math.pow (percentage, yearsLeft-3) + ... + amount*math.pow (percentage, yearsLeft-yearsLeft) ]

        Comment

        • Johan Ericsson

          #5
          what I meant was:
          (amount*math.po w(percentage, yearsLeft-n)) where n goes from 0 to yearsLeft

          Comment

          Working...