Need help with this expense calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Deathwing
    New Member
    • Mar 2007
    • 32

    Need help with this expense calculator

    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.
    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'
    
    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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by 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.
    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'
    
    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:
    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
    '''

    Comment

    • Deathwing
      New Member
      • Mar 2007
      • 32

      #3
      Originally posted by bvdet
      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:
      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 :)

      Comment

      • Deathwing
        New Member
        • Mar 2007
        • 32

        #4
        Originally posted by Deathwing
        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

        Code:
        # 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.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Originally posted by Deathwing
          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

          Code:
          # 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:
          Code:
          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:
          Code:
                  f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
          It looks like this when printed:
          Code:
          >>> 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
          >>>

          Comment

          • Deathwing
            New Member
            • Mar 2007
            • 32

            #6
            Originally posted by bvdet
            You can do this to save the itemized expenses:
            Code:
            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:
            Code:
                    f.write('Itemized Expenses -\n', '\n'.join(['%s: %0.2f' % (expense, cost) for expense, cost in expList])
            It looks like this when printed:
            Code:
            >>> 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.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Deathwing
              Thanks bv I'll take a look at it and play around with it. You're a great help.
              You are welcome Deathwing. Correction:
              Code:
              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.

              Comment

              • Deathwing
                New Member
                • Mar 2007
                • 32

                #8
                Originally posted by Deathwing
                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"
                Code:
                #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.")
                Last edited by Deathwing; May 11 '07, 09:38 PM. Reason: made a typo

                Comment

                • Deathwing
                  New Member
                  • Mar 2007
                  • 32

                  #9
                  Originally posted by Deathwing
                  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"
                  Code:
                  #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.

                  Comment

                  • ghostdog74
                    Recognized Expert Contributor
                    • Apr 2006
                    • 511

                    #10
                    Originally posted by Deathwing
                    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

                    Code:
                    # 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.
                    Code:
                    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

                    Comment

                    Working...