[Tutor] Help with Guess the number script

Alan Gauld alan.gauld at btinternet.com
Tue Mar 11 09:57:08 CET 2014


On 11/03/14 04:07, Scott W Dunning wrote:
>>> On Mar 8, 2014, at 3:57 AM, spir <denis.spir at gmail.com
>>> <mailto:denis.spir at gmail.com>> wrote:
>>>> And now that you have the right set of tests you can
>>>> half the number of lines by combining your if
>>>> conditions again, like you had in the original
>>>> post. ie. Bring your hot/cold/warm tests together.

I think that was me rather than Denis, but that's
irrelevant...

> This is what I have right now, obviously it’s not working.  I’ve been
> playing around with it but I’m just not seeing where I’m going wrong.
>   Any suggestions are greatly appreciated!
>
> def print_hints(secret, guess):
>      if guess < 1 or guess > 100:
>          print
>          print "Out of range!"
>          print
>      if guess < secret:
>          print
>          print "Too low!"
>      if guess > secret:
>          print
>          print "Too high!"

OK so far, you don't need all the print statements
but that's just a style issue. (You could just
insert '\n' characters instead.)

>      if guess < secret - 10 or guess > secret - 10:

This is the right idea for cutting the line count but you
have the comparison values wrong. Look back to earlier
emails, you are repeating the same error as before.
Manually think through what happens in the line above
if guess == secret.

And then once you get that fixed you can rewrite using
the chained comparison trick to tidy it up.

ie
if not (lower limit < guess > upper limit):

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list