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

Need help with this expense calculator

Deathwing
P: 32
Hey everyone, I have written a code that figures out a users total montly expenses. I have it working except for the fact that it does not add up their cumulative entries. For example, the code ask the user to enter their net montly income then an expense name and the cost of this expense it then ask them if they have another expense to enter if they hit no it calculates their monthly expenses by subtracting their net income from the expense and returns what money they have left over after expenses. If the user hits yes the program as them to enter an expense name and its cost. What I want to figure out is how to add up multiple expenses so that the user can keep entering as many expenses as they want and the program will properly calculate their total expenses and return what they have left over. Here is the code I have so far.
Thanks for your help.
Expand|Select|Wrap|Line Numbers
  1.  
  2. # Monthly Cost Calculator
  3.  
  4. print \
  5.       '''
  6. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  7. expenses subtracts them from your total montly income and lets you know what your
  8. total expenses are every month.
  9.         '''
  10.  
  11.  
  12. tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
  13.  
  14. reply='y'
  15.  
  16. while(reply!='n'):
  17.  
  18.     exp=raw_input("Please enter the name of the expense: ")
  19.     cost=float(raw_input("Please enter the cost of this expense: "))
  20.     reply=raw_input("Do you want to enter another expense y/n ? ")
  21.     if (reply == 'y'):
  22.         continue
  23.  
  24.     tot_exp=tk_home_pay-cost
  25.     print "Your totaly net income is:", tot_exp
  26.  
  27.  
  28.  
May 8 '07 #1
Share this Question
Share on Google+
9 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hey everyone, I have written a code that figures out a users total montly expenses. I have it working except for the fact that it does not add up their cumulative entries. For example, the code ask the user to enter their net montly income then an expense name and the cost of this expense it then ask them if they have another expense to enter if they hit no it calculates their monthly expenses by subtracting their net income from the expense and returns what money they have left over after expenses. If the user hits yes the program as them to enter an expense name and its cost. What I want to figure out is how to add up multiple expenses so that the user can keep entering as many expenses as they want and the program will properly calculate their total expenses and return what they have left over. Here is the code I have so far.
Thanks for your help.
Expand|Select|Wrap|Line Numbers
  1.  
  2. # Monthly Cost Calculator
  3.  
  4. print \
  5.       '''
  6. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  7. expenses subtracts them from your total montly income and lets you know what your
  8. total expenses are every month.
  9.         '''
  10.  
  11.  
  12. tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
  13.  
  14. reply='y'
  15.  
  16. while(reply!='n'):
  17.  
  18.     exp=raw_input("Please enter the name of the expense: ")
  19.     cost=float(raw_input("Please enter the cost of this expense: "))
  20.     reply=raw_input("Do you want to enter another expense y/n ? ")
  21.     if (reply == 'y'):
  22.         continue
  23.  
  24.     tot_exp=tk_home_pay-cost
  25.     print "Your totaly net income is:", tot_exp
  26.  
  27.  
  28.  
You were close! You were calculating tot_exp repeatedly as take home pay minus cost, but you never accumulated the costs. I took the liberty of modifying your code:
Expand|Select|Wrap|Line Numbers
  1. # Monthly Cost Calculator
  2.  
  3. print \
  4.       '''
  5. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  6. expenses subtracts them from your total montly income and lets you know what your
  7. total expenses are every month.
  8.         '''
  9.  
  10.  
  11. tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
  12.  
  13. reply='y'
  14. tot_exp = 0.0
  15. expList = []
  16.  
  17. while(reply!='n'):
  18.  
  19.     expList.append(raw_input("Please enter the name of the expense: "))
  20.     cost=float(raw_input("Please enter the cost of this expense: "))
  21.     reply=raw_input("Do you want to enter another expense y/n ? ")
  22.     tot_exp += cost
  23.     if reply != 'y':
  24.         print 'Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f' % (tk_home_pay, tot_exp, tk_home_pay-tot_exp)
  25.         print 'Expenses: %s' % ', '.join(expList)
  26.         break
  27.  
  28. '''
  29. >>> 
  30. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  31. expenses subtracts them from your total montly income and lets you know what your
  32. total expenses are every month.
  33.  
  34. Monthly take home pay: 2500.00
  35. Total expenses: 616.33
  36. Net income: 1883.67
  37. Expenses: beer, gas, tennis balls
  38. '''
May 8 '07 #2

Deathwing
P: 32
You were close! You were calculating tot_exp repeatedly as take home pay minus cost, but you never accumulated the costs. I took the liberty of modifying your code:
Expand|Select|Wrap|Line Numbers
  1. # Monthly Cost Calculator
  2.  
  3. print \
  4.       '''
  5. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  6. expenses subtracts them from your total montly income and lets you know what your
  7. total expenses are every month.
  8.         '''
  9.  
  10.  
  11. tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
  12.  
  13. reply='y'
  14. tot_exp = 0.0
  15. expList = []
  16.  
  17. while(reply!='n'):
  18.  
  19.     expList.append(raw_input("Please enter the name of the expense: "))
  20.     cost=float(raw_input("Please enter the cost of this expense: "))
  21.     reply=raw_input("Do you want to enter another expense y/n ? ")
  22.     tot_exp += cost
  23.     if reply != 'y':
  24.         print 'Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f' % (tk_home_pay, tot_exp, tk_home_pay-tot_exp)
  25.         print 'Expenses: %s' % ', '.join(expList)
  26.         break
  27.  
  28. '''
  29. >>> 
  30. Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
  31. expenses subtracts them from your total montly income and lets you know what your
  32. total expenses are every month.
  33.  
  34. Monthly take home pay: 2500.00
  35. Total expenses: 616.33
  36. Net income: 1883.67
  37. Expenses: beer, gas, tennis balls
  38. '''
You know bvdet, I knew that was the problem but I had no idea how to fix it, so thanks so much for showing me now I know, very nice now I can work on formatting how it prints out. Thanks again :)
May 8 '07 #3

Deathwing
P: 32
You know bvdet, I knew that was the problem but I had no idea how to fix it, so thanks so much for showing me now I know, very nice now I can work on formatting how it prints out. Thanks again :)
Hello I have a new question related to this one. I was playing around with the program as I thought it would be nice to have all the user input and results print out to a printable .txt file. After much toil I have come up with a solution. I know this is a stupid question but I was wondering if anyone could show me another perhaps more efficient way to come to the same conclusion. Here is the code aswell as the output. THANKS SO MUCH :D

Expand|Select|Wrap|Line Numbers
  1. # MEC_2.0
  2.  
  3. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  4.  
  5. reply ='y'
  6. tot_exp = 0.0
  7. expList = []
  8.  
  9. while(reply !='n'):
  10.  
  11.     expList.append(raw_input("Please enter the name of the expense: "))
  12.     cost=float(raw_input("Please enter the cost of this expense: "))
  13.     reply=raw_input("Do you want to enter another expense y/n ? ")
  14.     tot_exp += cost
  15.     if reply != 'y':
  16.         f=open('monthly_expenses.txt','a')
  17.         f.write('MEC_2.0: Monthly Expense Report\n')
  18.         f.write('-'*60+'\n')
  19.         f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
  20.                      % (net_income,tot_exp,net_income-tot_exp))
  21.         f.write('-'*60+'\n')
  22.         f.writelines('Expenses: %s\n' % ', '.join(expList))
  23.         f.write('-'*60+'\n')
  24.  
  25.  
  26. f.close()
  27.  
  28. raw_input("Press enter to exit the program.")
  29.  
  30.  
  31. OutPut:
  32.  
  33. MEC_2.0: Monthly Expense Report
  34. ------------------------------------------------------------
  35. Monthly take home pay: 3000.00
  36. Total expenses: 1333.00
  37. Net income: 1667.00
  38. ------------------------------------------------------------
  39. Expenses: Home , Car, Car Insurance
  40. ------------------------------------------------------------
  41.  
  42.  
Also how could I change the code so that in the ".txt" the Total expenses is broken down into ie) Home:900.00, Car:300.00 etc... Once again thanks for all of the help.
May 11 '07 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Hello I have a new question related to this one. I was playing around with the program as I thought it would be nice to have all the user input and results print out to a printable .txt file. After much toil I have come up with a solution. I know this is a stupid question but I was wondering if anyone could show me another perhaps more efficient way to come to the same conclusion. Here is the code aswell as the output. THANKS SO MUCH :D

Expand|Select|Wrap|Line Numbers
  1. # MEC_2.0
  2.  
  3. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  4.  
  5. reply ='y'
  6. tot_exp = 0.0
  7. expList = []
  8.  
  9. while(reply !='n'):
  10.  
  11.     expList.append(raw_input("Please enter the name of the expense: "))
  12.     cost=float(raw_input("Please enter the cost of this expense: "))
  13.     reply=raw_input("Do you want to enter another expense y/n ? ")
  14.     tot_exp += cost
  15.     if reply != 'y':
  16.         f=open('monthly_expenses.txt','a')
  17.         f.write('MEC_2.0: Monthly Expense Report\n')
  18.         f.write('-'*60+'\n')
  19.         f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
  20.                      % (net_income,tot_exp,net_income-tot_exp))
  21.         f.write('-'*60+'\n')
  22.         f.writelines('Expenses: %s\n' % ', '.join(expList))
  23.         f.write('-'*60+'\n')
  24.  
  25.  
  26. f.close()
  27.  
  28. raw_input("Press enter to exit the program.")
  29.  
  30.  
  31. OutPut:
  32.  
  33. MEC_2.0: Monthly Expense Report
  34. ------------------------------------------------------------
  35. Monthly take home pay: 3000.00
  36. Total expenses: 1333.00
  37. Net income: 1667.00
  38. ------------------------------------------------------------
  39. Expenses: Home , Car, Car Insurance
  40. ------------------------------------------------------------
  41.  
  42.  
Also how could I change the code so that in the ".txt" the Total expenses is broken down into ie) Home:900.00, Car:300.00 etc... Once again thanks for all of the help.
You can do this to save the itemized expenses:
Expand|Select|Wrap|Line Numbers
  1. while(reply !='n'):
  2.  
  3.     exp = raw_input("Please enter the name of the expense: ")
  4.     cost=float(raw_input("Please enter the cost of this expense: "))
  5.     expList.append([exp, cost])
  6.     reply=raw_input("Do you want to enter another expense y/n ? ")
  7.     tot_exp += cost
To write to file:
Expand|Select|Wrap|Line Numbers
  1.         f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
It looks like this when printed:
Expand|Select|Wrap|Line Numbers
  1. >>> print 'Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
  2. Itemized Expenses -
  3. beer: 100.00
  4. tennis balls: 200.00
  5. >>> 
May 11 '07 #5

Deathwing
P: 32
You can do this to save the itemized expenses:
Expand|Select|Wrap|Line Numbers
  1. while(reply !='n'):
  2.  
  3.     exp = raw_input("Please enter the name of the expense: ")
  4.     cost=float(raw_input("Please enter the cost of this expense: "))
  5.     expList.append([exp, cost])
  6.     reply=raw_input("Do you want to enter another expense y/n ? ")
  7.     tot_exp += cost
To write to file:
Expand|Select|Wrap|Line Numbers
  1.         f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
It looks like this when printed:
Expand|Select|Wrap|Line Numbers
  1. >>> print 'Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
  2. Itemized Expenses -
  3. beer: 100.00
  4. tennis balls: 200.00
  5. >>> 
Thanks bv I'll take a look at it and play around with it. You're a great help.
May 11 '07 #6

bvdet
Expert Mod 2.5K+
P: 2,851
Thanks bv I'll take a look at it and play around with it. You're a great help.
You are welcome Deathwing. Correction:
Expand|Select|Wrap|Line Numbers
  1. f.write('Itemized Expenses -\n'+'\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList]))
The file.write() method only accepts one argument.
May 11 '07 #7

Deathwing
P: 32
Thanks bv I'll take a look at it and play around with it. You're a great help.
hmm I can't seem to this this version to work ? This is what I got.
suggestions ? I keep getting an error "Token Error: EOF in multi-line statement"
Expand|Select|Wrap|Line Numbers
  1. #mec_2.1
  2. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  3.  
  4. reply ='y'
  5. tot_exp = 0.0
  6. expList = []
  7.  
  8.  
  9. while(reply !='n'):
  10.  
  11.     exp = raw_input("Please enter the name of the expense: ")
  12.     cost=float(raw_input("Please enter the cost of this expense: "))
  13.     expList.append([exp, cost])
  14.     reply=raw_input("Do you want to enter another expense y/n ? ")
  15.     tot_exp += cost
  16.  
  17.     f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (exp, cost) for exp, cost in expList])
  18.  
  19. raw_input("Press Enter to exit.")
  20.  
  21.  
  22.  
  23.  
May 11 '07 #8

Deathwing
P: 32
hmm I can't seem to this this version to work ? This is what I got.
suggestions ? I keep getting an error "Token Error: EOF in multi-line statement"
Expand|Select|Wrap|Line Numbers
  1. #mec_2.1
  2. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  3.  
  4. reply ='y'
  5. tot_exp = 0.0
  6. expList = []
  7.  
  8.  
  9. while(reply !='n'):
  10.  
  11.     exp = raw_input("Please enter the name of the expense: ")
  12.     cost=float(raw_input("Please enter the cost of this expense: "))
  13.     expList.append([exp, cost])
  14.     reply=raw_input("Do you want to enter another expense y/n ? ")
  15.     tot_exp += cost
  16.  
  17.     f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (exp, cost) for exp, cost in expList])
  18.  
  19. raw_input("Press Enter to exit.")
  20.  
  21.  
  22.  
  23.  
SO SORRY BV.. I didn't notice your correction till after I sent my question I'll be more careful next time.
May 11 '07 #9

Expert 100+
P: 511
Hello I have a new question related to this one. I was playing around with the program as I thought it would be nice to have all the user input and results print out to a printable .txt file. After much toil I have come up with a solution. I know this is a stupid question but I was wondering if anyone could show me another perhaps more efficient way to come to the same conclusion. Here is the code aswell as the output. THANKS SO MUCH :D

Expand|Select|Wrap|Line Numbers
  1. # MEC_2.0
  2.  
  3. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  4.  
  5. reply ='y'
  6. tot_exp = 0.0
  7. expList = []
  8.  
  9. while(reply !='n'):
  10.  
  11.     expList.append(raw_input("Please enter the name of the expense: "))
  12.     cost=float(raw_input("Please enter the cost of this expense: "))
  13.     reply=raw_input("Do you want to enter another expense y/n ? ")
  14.     tot_exp += cost
  15.     if reply != 'y':
  16.         f=open('monthly_expenses.txt','a')
  17.         f.write('MEC_2.0: Monthly Expense Report\n')
  18.         f.write('-'*60+'\n')
  19.         f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
  20.                      % (net_income,tot_exp,net_income-tot_exp))
  21.         f.write('-'*60+'\n')
  22.         f.writelines('Expenses: %s\n' % ', '.join(expList))
  23.         f.write('-'*60+'\n')
  24.  
  25.  
  26. f.close()
  27.  
  28. raw_input("Press enter to exit the program.")
  29.  
  30.  
  31. OutPut:
  32.  
  33. MEC_2.0: Monthly Expense Report
  34. ------------------------------------------------------------
  35. Monthly take home pay: 3000.00
  36. Total expenses: 1333.00
  37. Net income: 1667.00
  38. ------------------------------------------------------------
  39. Expenses: Home , Car, Car Insurance
  40. ------------------------------------------------------------
  41.  
  42.  
Also how could I change the code so that in the ".txt" the Total expenses is broken down into ie) Home:900.00, Car:300.00 etc... Once again thanks for all of the help.
use a while true loop and break out upon the condition. (although its just a matter of taste, i find while True loop more "neat" ). you can also store all your output lines into memory, before writing out in one statement.
Expand|Select|Wrap|Line Numbers
  1.  
  2. net_income=float(raw_input("Please enter your total monthly take home pay: "))
  3. tot_exp = 0.0
  4. expList = []
  5. while 1:
  6.     expList.append(raw_input("Please enter the name of the expense: "))
  7.     cost=float(raw_input("Please enter the cost of this expense: "))
  8.     reply=raw_input("Do you want to enter another expense y/n ? ")
  9.     tot_exp += cost
  10.     if reply in ["n","N"]:
  11.         f=open('monthly_expenses.txt','a')
  12.         writeout = """
  13.         MEC_2.0: Monthly Expense Report
  14.         %s
  15.         Monthly take home pay: %0.2f
  16.         Total expenses: %0.2f
  17.         Net income: %0.2f
  18.         %s
  19.         Expenses: %s
  20.         %s
  21.         """ % ('-'*60 , net_income,tot_exp,net_income-tot_exp,'-'*60, ', '.join(expList) ,'-'*60) 
  22.         f.write(writeout)
  23.         f.close()
  24.         break
  25.  
May 12 '07 #10

Post your reply

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