443,965 Members | 1,690 Online
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

9 Replies

 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 # 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. May 11 '07 #4

 P: 32 You can do this to save the itemized expenses: Expand|Select|Wrap|Line Numbers 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: Expand|Select|Wrap|Line Numbers         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 >>> 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. May 11 '07 #6

 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 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

 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 #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.")         May 11 '07 #8

 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 #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. May 11 '07 #9