[prog] Re: [Courses]Need a little help with extremely basic
Python programming
Sue Stones
suzo at bigpond.net.au
Fri Aug 22 19:27:22 EST 2003
G'day Jason,
May I suggest an improvement to your program? I would do the test (number >
remaining) before adding number to sum. That way if it is too large you
don't need to add it then subtact it agian. So you onlly add number to sum
if it is apropriate to your aim.
The order of performing operations is important. Firstly it reduces the
posibilities mistakes, (or rounding errors sometimes), and secondally it
avoids doing too much work. With a little program like this the latter is
not very important, but if you were doing something like this on a large set
of numbers it could make a very significant diference.
In short it is an important skill as a programmer to consider the order of the
steps involved and so producing a better "algorithm".
sue
On Fri, 22 Aug 2003 08:30 am, Mary wrote:
> Hi Jason,
>
> You might be better off asking your question on linuxchix's
> programming at linuxchix.org mailing list (Cc-ed).
>
> -Mary
>
> On Thu, Aug 21, 2003, Jason Kappel wrote:
> > I have a quick question about the program in question. I wrote a
> > version that not only adds up to 100, but won't let the user go over,
> > and now I'm looking for a way to check if the input is actually a
> > number, so I don't get an error when text is entered as an input. Is
> > there a function to check if an input is either an integer or floating
> > point?
> >
> > #this takes number inputs from the user untill the sum reaches 100
> > sum = 0.0
> > while (sum < 100):
> > remaining = 100.0-sum
> > print "Please choose a number no larger than",remaining
> > number = input ("> ")
> > sum = sum+number
> > while (number > remaining): #while input too large, subtract and
> > try again
> > sum = sum-number
> > print "That was bigger than",remaining,"try again"
> > number = input ("> ")
> > sum = sum+number
> > print "Good for you! You can add to",sum
> >
> >
> > I have a quick question
> > _________________________________________________________________
> >
> > [1]MSN 8: Get 6 months for $9.95/month.
> >
> > References
> >
> > 1. http://g.msn.com/8HMFENUS/2728??PS=
> > --
> > Courses mailing list
> > Courses at linuxchix.org
> > http://mailman.linuxchix.org/mailman/listinfo/courses
>
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
More information about the Programming
mailing list