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

How to create a pension calculator?

P: n/a
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...

Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. class Pension:
  4.  
  5.     def __init__(self, name, age, amount):
  6.         self.name = name
  7.         self.age = age
  8.         self.amount = amount
  9.  
  10.  
  11. def percentage():
  12.     p = float(raw_input('How many percent is the growth? '))
  13.     percentage = (p/100)+1
  14.     return percentage
  15.  
  16.  
  17. def read():
  18.     f = open('file.txt','r')
  19.     rad = f.readline()
  20.     info = list()
  21.     while rad != '':
  22.         rad = rad.rstrip('\n')
  23.         parts = rad.split('/')
  24.         name = str(parts[0])
  25.         age = int(parts[1])
  26.         amount = float(parts[2])
  27.         temp = Pension(name,age,amount)
  28.         info.append(temp)
  29.         rad = f.readline()
  30.     f.close()
  31.     return info
  32.  
  33.  
  34. def printing(info):
  35.     for i in range(len(info)):
  36.         print info[i]
  37.  
  38.  
  39.  
  40. percentage()
  41. info = read()
  42. printing(info)
  43.  
  44.  
I would love it if anyone of you could help me out a bit :)
Thanks a lot in advance.
Nov 3 '10 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 621
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.
Expand|Select|Wrap|Line Numbers
  1. def printing(info):
  2.     for class_instance in info:
  3.         print class_instance.name, class_instance.age, \
  4.               class_instance.amount
  5. ##
  6. ##   you could also add a print method to the class 
  7. ##
  8. class Pension:
  9.  
  10.     def __init__(self, name, age, amount):
  11.         self.name = name
  12.         self.age = age
  13.         self.amount = amount
  14.  
  15.     def print_data(self):
  16.         print self.name, self.age, self.amount
  17.  
  18. def printing(info):
  19.     for class_instance in info:
  20.         class_instance.print_data() 
Nov 3 '10 #2

P: n/a
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.
Nov 4 '10 #3

P: n/a
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) ]
Nov 5 '10 #4

P: n/a
what I meant was:
(amount*math.pow(percentage, yearsLeft-n)) where n goes from 0 to yearsLeft
Nov 5 '10 #5

Post your reply

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