Hi Michael Scott Thank you and yes I agree there is room for user error. I am going to add a validator but I wanted to get the first part right. I appreciate your comment on the white space too.
Being a beginner I find it easier to write the main thing I want the code to do and then add extra to make it work efficiently. Perhaps not what a programmer does but it helps me this way. Thanks so much. Leonie -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of [email protected] Sent: Saturday, 16 April 2011 9:43 AM To: [email protected] Subject: Tutor Digest, Vol 86, Issue 57 Send Tutor mailing list submissions to [email protected] To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to [email protected] You can reach the person managing the list at [email protected] When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: Help - accumulator not working (Lea) (michael scott) 2. Re: Help - accumulator not working (Lea) (Alan Gauld) 3. Re: than "Re: Contents of Tutor digest..." Tutor Digest, Vol 86, Issue 56 (Lea Parker) ---------------------------------------------------------------------- Message: 1 Date: Fri, 15 Apr 2011 15:46:32 -0700 (PDT) From: michael scott <[email protected]> To: [email protected] Subject: Re: [Tutor] Help - accumulator not working (Lea) Message-ID: <[email protected]> Content-Type: text/plain; charset="utf-8" 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 <[email protected]> To: [email protected] 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/attach ment-0001.html> ------------------------------ Message: 2 Date: Sat, 16 Apr 2011 00:27:22 +0100 From: "Alan Gauld" <[email protected]> To: [email protected] Subject: Re: [Tutor] Help - accumulator not working (Lea) Message-ID: <[email protected]> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Lea Parker" <[email protected]> wrote > 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. You have to write the code to add each value to the accumulator Your loop does not do that.... > """This program is to calculate if the user is over or under budget > > for the month""" > > > > Wow! Thats a lot of whitespace. It is good to separate code blocks into logical segments with whitespace, but too much of it just makes the code flow hard to see. In the old days of green screen terminals on mainframes they used to say that a function should all fit on a single screen - 24 lines. Nowadays we don't need to be quite so penny pinching, but the concept of seeing the whole flow in one place is a good one. I'll remove some excess space below... > def main(): > # Create an accumulator Oh, and if you can use a good variable name to describe the variable you don't need a comment either - It's just more distracting wasted space. Comments are to explain *why* (and occasionally, for the really obscure, how), but good names describe what. > total_expense = 0.0 > budget = float(raw_input('Enter the amount of your budget for the > month:')) > > # Calculate a series of expenses I left this comment because it explains why we have a loop... > expense = float(raw_input('Enter your first expense $')) > total_expense = total_expense + expense > while expense != 0: > expense = float(raw_input('Enter the next expense or 0 to > finish')) > surplus = budget - total_expense > > print 'Your total expenses for the month $', total_expense > print 'Your surplus amount after expenses $', surplus > > main() Hopefully that makes it easier to see what you missed out? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ------------------------------ Message: 3 Date: Sat, 16 Apr 2011 09:42:41 +1000 From: "Lea Parker" <[email protected]> To: <[email protected]> Subject: Re: [Tutor] than "Re: Contents of Tutor digest..." Tutor Digest, Vol 86, Issue 56 Message-ID: <[email protected]> Content-Type: text/plain; charset="us-ascii" Thank you message 4 this has solved my problem I can now work out the next part of my program. Thank you so much. -----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of [email protected] Sent: Saturday, 16 April 2011 8:47 AM To: [email protected] Subject: Tutor Digest, Vol 86, Issue 56 Send Tutor mailing list submissions to [email protected] To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to [email protected] You can reach the person managing the list at [email protected] When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Help to explain commenting (Andr?s Chand?a) 2. Re: Python on TV (Alan Gauld) 3. Help - accumulator not working (Lea) (Lea Parker) 4. Re: Help - accumulator not working (Lea) (Joel Goldstick) ---------------------------------------------------------------------- Message: 1 Date: Fri, 15 Apr 2011 18:21:43 +0200 From: "Andr?s Chand?a" <[email protected]> Subject: [Tutor] Help to explain commenting Message-ID: <[email protected]> Content-Type: text/plain; charset="iso-8859-1" Hello everybody, I could finally complete succesfully the code I was working with. As I'm quite new to python, many of the things I have in my code are copied from different sources, and I do not undertand all of them, well, I have to deliver this code for a project, and in the best documented way that I could, I already commented all that I know, and I suppouse wrongly in some parts. So the request is, if you can take a look at the code, comment the parts that are not yet commented, correct the errors, and propouse some improvement in the parts you think diserves it. I attach the code in a tgz file, if the attached can not be seen then this link: http://www.chandia.net/compart/NMT-2.4-20110415.tar.gz Thanks in advance to all of you and to the people that already helped me. _______________________ ????????????andr?s chand?a P No imprima innecesariamente. ?Cuide el medio ambiente! -------------- next part -------------- A non-text attachment was scrubbed... Name: NMT-2.4-20110415.tar.gz Type: application/x-gzip Size: 4177 bytes Desc: not available URL: <http://mail.python.org/pipermail/tutor/attachments/20110415/937a8c20/attach ment-0001.bin> ------------------------------ Message: 2 Date: Fri, 15 Apr 2011 20:35:14 +0100 From: "Alan Gauld" <[email protected]> To: [email protected] Subject: Re: [Tutor] Python on TV Message-ID: <[email protected]> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response "bob gailer" <[email protected]> wrote >> The show should be here - Pause at 1 minute 20 for the Python >> screnshot: >> >> http://fwd.channel5.com/gadget-show/videos/challenge/surprise-special >> -part-4 > > I am told "the video ... cannot be viewed from your currrent country > ..." I don't know if YouTube will be any more obliging but try this: http://www.youtube.com/watch?v=oDNZP1X30WE&list=SL Python can be seen at around 29 mins 45 secs... Enjoy (I hope) Alan G. ------------------------------ Message: 3 Date: Sat, 16 Apr 2011 07:52:22 +1000 From: "Lea Parker" <[email protected]> To: <[email protected]> Subject: [Tutor] Help - accumulator not working (Lea) Message-ID: <[email protected]> Content-Type: text/plain; charset="us-ascii" 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/attach ment-0001.html> ------------------------------ Message: 4 Date: Fri, 15 Apr 2011 18:47:21 -0400 From: Joel Goldstick <[email protected]> To: [email protected] Subject: Re: [Tutor] Help - accumulator not working (Lea) Message-ID: <[email protected]> Content-Type: text/plain; charset="utf-8" On Fri, Apr 15, 2011 at 5:52 PM, Lea Parker <[email protected]> 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 - [email protected] > 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/attach ment.html> ------------------------------ _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 86, Issue 56 ************************************* ------------------------------ _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 86, Issue 57 ************************************* _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
