[Tutor] Help - accumulator not working (Lea)

Lea Parker lea-parker at bigpond.com
Fri Apr 15 23:52:22 CEST 2011


Hello

 

I am trying to create this program for a uni assignment. I cannot get it to
add the expenses to the accumulator I have set. Would you mind having a look
and letting me know if I have something in the wrong place or indented
incorrectly. Perhaps I am missing something.

 

There could be other things wrong but I need to fix this first and then I
can focus on the next thing. I have difficulty trying to fix lots of things
at once so if you could just comment on the problem and I will ask again if
I can't work out the next problem I have. I like to have a go myself first.
J

 

My code is:

 

"""This program is to calculate if the user is over or under budget

for the month"""

 

 

def main():

  

    # Create an accumulator

    total_expense = 0.0

 

    # Ask user for the monthly budget

    budget = float(raw_input('Enter the amount of your budget for the month:
$'))

    

 

    # Calculate a series of expenses

    expense = float(raw_input('Enter your first expense $'))

    

     # Accumlate expense

    total_expense = total_expense + expense

 

    # Continue processing as long as the user

    # does not enter 0

    while expense != 0:

 

        #Get another expense

        expense = float(raw_input('Enter the next expense or 0 to finish
$'))

           

        #Calculate surplus

        surplus = budget - total_expense

 

    #Display results

    print 'Your total expenses for the month $', total_expense

    print 'Your surplus amount after expenses $', surplus

 

# Call the main function.

main()

 

Thank you.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110416/b09f472e/attachment.html>


More information about the Tutor mailing list