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. -
-
# Monthly Cost Calculator
-
-
print \
-
'''
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
'''
-
-
-
tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply='y'
-
-
while(reply!='n'):
-
-
exp=raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
if (reply == 'y'):
-
continue
-
-
tot_exp=tk_home_pay-cost
-
print "Your totaly net income is:", tot_exp
-
-
-
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. -
-
# Monthly Cost Calculator
-
-
print \
-
'''
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
'''
-
-
-
tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply='y'
-
-
while(reply!='n'):
-
-
exp=raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
if (reply == 'y'):
-
continue
-
-
tot_exp=tk_home_pay-cost
-
print "Your totaly net income is:", tot_exp
-
-
-
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: - # Monthly Cost Calculator
-
-
print \
-
'''
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
'''
-
-
-
tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply='y'
-
tot_exp = 0.0
-
expList = []
-
-
while(reply!='n'):
-
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply != 'y':
-
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)
-
print 'Expenses: %s' % ', '.join(expList)
-
break
-
-
'''
-
>>>
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
-
Monthly take home pay: 2500.00
-
Total expenses: 616.33
-
Net income: 1883.67
-
Expenses: beer, gas, tennis balls
-
'''
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: - # Monthly Cost Calculator
-
-
print \
-
'''
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
'''
-
-
-
tk_home_pay=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply='y'
-
tot_exp = 0.0
-
expList = []
-
-
while(reply!='n'):
-
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply != 'y':
-
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)
-
print 'Expenses: %s' % ', '.join(expList)
-
break
-
-
'''
-
>>>
-
Welcome to Monthly expense Calculator 1.0. This program adds up your total montly
-
expenses subtracts them from your total montly income and lets you know what your
-
total expenses are every month.
-
-
Monthly take home pay: 2500.00
-
Total expenses: 616.33
-
Net income: 1883.67
-
Expenses: beer, gas, tennis balls
-
'''
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 :)
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 - # MEC_2.0
-
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply ='y'
-
tot_exp = 0.0
-
expList = []
-
-
while(reply !='n'):
-
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply != 'y':
-
f=open('monthly_expenses.txt','a')
-
f.write('MEC_2.0: Monthly Expense Report\n')
-
f.write('-'*60+'\n')
-
f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
-
% (net_income,tot_exp,net_income-tot_exp))
-
f.write('-'*60+'\n')
-
f.writelines('Expenses: %s\n' % ', '.join(expList))
-
f.write('-'*60+'\n')
-
-
-
f.close()
-
-
raw_input("Press enter to exit the program.")
-
-
-
OutPut:
-
-
MEC_2.0: Monthly Expense Report
-
------------------------------------------------------------
-
Monthly take home pay: 3000.00
-
Total expenses: 1333.00
-
Net income: 1667.00
-
------------------------------------------------------------
-
Expenses: Home , Car, Car Insurance
-
------------------------------------------------------------
-
-
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.
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 - # MEC_2.0
-
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply ='y'
-
tot_exp = 0.0
-
expList = []
-
-
while(reply !='n'):
-
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply != 'y':
-
f=open('monthly_expenses.txt','a')
-
f.write('MEC_2.0: Monthly Expense Report\n')
-
f.write('-'*60+'\n')
-
f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
-
% (net_income,tot_exp,net_income-tot_exp))
-
f.write('-'*60+'\n')
-
f.writelines('Expenses: %s\n' % ', '.join(expList))
-
f.write('-'*60+'\n')
-
-
-
f.close()
-
-
raw_input("Press enter to exit the program.")
-
-
-
OutPut:
-
-
MEC_2.0: Monthly Expense Report
-
------------------------------------------------------------
-
Monthly take home pay: 3000.00
-
Total expenses: 1333.00
-
Net income: 1667.00
-
------------------------------------------------------------
-
Expenses: Home , Car, Car Insurance
-
------------------------------------------------------------
-
-
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: - while(reply !='n'):
-
-
exp = raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
expList.append([exp, cost])
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
To write to file: - f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
It looks like this when printed: - >>> print 'Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
-
Itemized Expenses -
-
beer: 100.00
-
tennis balls: 200.00
-
>>>
You can do this to save the itemized expenses: - while(reply !='n'):
-
-
exp = raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
expList.append([exp, cost])
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
To write to file: - f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
It looks like this when printed: - >>> print 'Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
-
Itemized Expenses -
-
beer: 100.00
-
tennis balls: 200.00
-
>>>
Thanks bv I'll take a look at it and play around with it. You're a great help.
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: - 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.
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" - #mec_2.1
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply ='y'
-
tot_exp = 0.0
-
expList = []
-
-
-
while(reply !='n'):
-
-
exp = raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
expList.append([exp, cost])
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
-
f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (exp, cost) for exp, cost in expList])
-
-
raw_input("Press Enter to exit.")
-
-
-
-
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" - #mec_2.1
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply ='y'
-
tot_exp = 0.0
-
expList = []
-
-
-
while(reply !='n'):
-
-
exp = raw_input("Please enter the name of the expense: ")
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
expList.append([exp, cost])
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
-
f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (exp, cost) for exp, cost in expList])
-
-
raw_input("Press Enter to exit.")
-
-
-
-
SO SORRY BV.. I didn't notice your correction till after I sent my question I'll be more careful next time.
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 - # MEC_2.0
-
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
-
reply ='y'
-
tot_exp = 0.0
-
expList = []
-
-
while(reply !='n'):
-
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply != 'y':
-
f=open('monthly_expenses.txt','a')
-
f.write('MEC_2.0: Monthly Expense Report\n')
-
f.write('-'*60+'\n')
-
f.writelines('Monthly take home pay: %0.2f\nTotal expenses: %0.2f\nNet income: %0.2f\n'
-
% (net_income,tot_exp,net_income-tot_exp))
-
f.write('-'*60+'\n')
-
f.writelines('Expenses: %s\n' % ', '.join(expList))
-
f.write('-'*60+'\n')
-
-
-
f.close()
-
-
raw_input("Press enter to exit the program.")
-
-
-
OutPut:
-
-
MEC_2.0: Monthly Expense Report
-
------------------------------------------------------------
-
Monthly take home pay: 3000.00
-
Total expenses: 1333.00
-
Net income: 1667.00
-
------------------------------------------------------------
-
Expenses: Home , Car, Car Insurance
-
------------------------------------------------------------
-
-
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. -
-
net_income=float(raw_input("Please enter your total monthly take home pay: "))
-
tot_exp = 0.0
-
expList = []
-
while 1:
-
expList.append(raw_input("Please enter the name of the expense: "))
-
cost=float(raw_input("Please enter the cost of this expense: "))
-
reply=raw_input("Do you want to enter another expense y/n ? ")
-
tot_exp += cost
-
if reply in ["n","N"]:
-
f=open('monthly_expenses.txt','a')
-
writeout = """
-
MEC_2.0: Monthly Expense Report
-
%s
-
Monthly take home pay: %0.2f
-
Total expenses: %0.2f
-
Net income: %0.2f
-
%s
-
Expenses: %s
-
%s
-
""" % ('-'*60 , net_income,tot_exp,net_income-tot_exp,'-'*60, ', '.join(expList) ,'-'*60)
-
f.write(writeout)
-
f.close()
-
break
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
...
|
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...
|
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...
|
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...
|
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" ...
|
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...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |