[Tutor] Help - accumulator not working (Lea)
Joel Goldstick
joel.goldstick at gmail.com
Sat Apr 16 00:47:21 CEST 2011
On Fri, Apr 15, 2011 at 5:52 PM, Lea Parker <lea-parker at bigpond.com> wrote:
> 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: $'))
>
>
>
above here is good
>
>
> # Calculate a series of expenses
>
> expense = float(raw_input('Enter your first expense $'))
>
>
>
I would remove the input above and move it to your loop.
> # Accumlate expense
>
> total_expense = total_expense + expense
>
>
above you don't need this since you haven't added anything yet (see below)
>
>
I set expense to 1 just to get the loop started. It could be anything but 0
> # 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.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
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:
$'))
expense = 1
total_expense = 0
while expense != 0:
#Get another expense
expense = float(raw_input('Enter the next expense or 0 to finish
$'))
#Calculate surplus
total_expense = total_expense + expense
surplus = budget - total_expense
print budget, total_expense
#Display results
print 'Your total expenses for the month $', total_expense
print 'Your surplus amount after expenses $', surplus
main()
Good luck with your course
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110415/1b6d83b6/attachment-0001.html>
More information about the Tutor
mailing list