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 3814 bvdet 2,851
Recognized Expert Moderator Specialist
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
Recognized Expert Moderator Specialist
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
Recognized Expert Moderator Specialist
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 and come up
with a dollar amount.
If anyone out there sees what I am doing wrong- could you offer some
advice? I apologize in advance for adding the entire code- I know it
is more than most of you are willing to look at, but I just can't...
|
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 use the calculator.
"document.Calculate.Input is null or not an object"
Can someone please tell me why this is?
|
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 Expenses.Debtors, Expenses.Cash
PIVOT Format(,"mmm") In
("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
I can't figure this out and I feel pretty dumb about it.
|
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....
#include <stdio.h>
#include <stdlib.h>
|
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 happend. I think
the problem may be in my input function or in the main function.
If anyone out there can help me it woul be greatly appreciated.
Here is the code:
#include <iostream>
| |
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 java.awt.*;
import java.awt.event.*;
import java.io.*;
|
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 Population Calculator on the javascripkit.com site. The Calculator functions just as I imagine I want my number generator to function, but I need it to present a different number.
I work for a non profit organization that administers Head Start and...
|
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" "Balance"
Expense #1 $4,000 $6,000
Expense #2 $50 $5,950
Expense #3 $100 $5,850
|
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 and an operation, with and output of the answer with the original equation. I think I need to use the split function, however I dont know what to do once it is split. Here is what I have so far-
import javax.swing.*;
import java.awt.*;
import...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |