[Tutor] Help - accumulator not working (Lea)

michael scott jigenbakuda at yahoo.com
Sat Apr 16 00:46:32 CEST 2011


Hi Lea, how are you today?

Well please keep in mind that nothing is "wrong" with your code,  its doing 
exactly what you asked it to do. But I would call your attention to  your while 
loop, you want to accumulate things, but may I ask exactly what are you 
accumulating in your loop?

Also quite by accident I entered 00 as my budget and I got a negative surplus, 
lol. Perhaps you should implement something that ensures that a (stupid) user 
like myself does not enter a 0- or negative value for the budget. Just a 
thought...

To help me attempt to understand the small programs I write, I pretend that I'm 
the computer and I literally compute  the program as if I was the interpreter, I 
follow each line of my code to truly understand it. Perhaps with these gentle 
nudges you will solve your problem :)

 ----
What is it about you... that intrigues me so?




________________________________
From: Lea Parker <lea-parker at bigpond.com>
To: tutor at python.org
Sent: Fri, April 15, 2011 5:52:22 PM
Subject: [Tutor] Help - accumulator not working (Lea)


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/20110415/d62c34c3/attachment.html>


More information about the Tutor mailing list