[Tutor] Is this overkill?

Python python at venix.com
Sat Jan 21 19:24:36 CET 2006


On Sat, 2006-01-21 at 10:09 -0500, Bradly McConnell wrote:
> On 1/21/06, Alan Gauld <alan.gauld at freenet.co.uk> wrote:
> > number = input("Please enter a number: ")
> > while number != 100:
> >     additional_number = input("Please enter an additional number: ")
> >     if additional_number + number > 100:
> >         lower_number = input("please enter a lower number: ")
> >
> > you can just 'continue' here since the while loop asks for a new
> > number anyhow. It xchanges the behaviour salightly in that it never
> > allows a number that sums to more than 100 whereas you allow
> > only two attempts.
> 
> Not sure where I went wrong, but at this point, I cannot get the count
> to reach 100 unless I use something like 60 and 40 for the number a
> additional_number inputs.  It seems that my variables are being
> overwritten, but the "previous" additional number doesn't get added to
> number before it happens.  This appears in both my original, and with
> the modifications that you (Alan) suggested.

One approach that might help simplify things would be to use a variable
for the input prompt.  Changing as little as possible:

prompt = "Please enter an additional number: "
while number != 100:
    additional_number = input(prompt)

This helps remove the need for additional input requests and should let
you keep the while loop logic reasonably simple.  Do this in conjunction
with Alan's advice.  I think we are both trying to push you in the same
direction.

> 
> I think I worked on it a bit too much last night, and lost track of
> what I tried and didn't try so I think I'll take a look through the
> areas on loops and conditional statements again this evening and start
> over "fresh".
> 
> Thanks for the help.
> 
> Brad
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 
Lloyd Kvam
Venix Corp



More information about the Tutor mailing list