473,385 Members | 1,546 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,385 software developers and data experts.

Need help with this expense calculator

Deathwing
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
9 3791
bvdet
2,851 Expert Mod 2GB
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
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
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
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
ghostdog74
511 Expert 256MB
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

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

Similar topics

4
by: Kim14 | last post by:
I have a table that works fine in IE, but doesn't work in Netscape or Firefox. It should automatically come up with numbers in some of the fields and depending what is entered, it should calculate...
3
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
2
by: Don Sealer | last post by:
How would I make this query sum the fields "expense"+"cash" TRANSFORM Sum(Expenses.Expense) AS SumOfExpense SELECT Expenses.Debtors, Expenses.Cash, Sum(Expenses.Expense) AS FROM Expenses GROUP...
6
by: Rafael | last post by:
Hi Everyone, I need some help with my calculator program. I need my program to do 2 arguments and a 3rd, but the 3rd with different operators. Any help would be great. Here is my code.... ...
13
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else...
19
by: TexasNewbie | last post by:
This was originally just a calculator without a decimal point. After I added the decimal, it now tells me invalid second number. //GUI Calculator Program import javax.swing.*; import...
2
by: masker | last post by:
I was on the web trying to find a javascript that would allow me to input a number on my website and have it increase by a given percentage every second. During my search I found the Earth...
2
by: EManning | last post by:
Using A2K. I've got a report/subreport that looks like the following: <main report> Account AAA Beginning Fund Balance: $10,000 <subreport> "Expense" "Total" ...
17
by: Max347 | last post by:
This is my first post, so hopefully I can give enough information. I am running windows xp, using jGrasp to write code. I need to make a calculator in which the user inputs 2 floating point numbers...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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?

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.