[Tutor] Help with Guess the number script

Scott W Dunning swdunning at cox.net
Tue Mar 11 05:07:43 CET 2014


>> On Mar 8, 2014, at 3:57 AM, spir <denis.spir at gmail.com> wrote:
>>> 
>>> Well done.
>>> 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.

So below is what I finally came up with that works.  I’m trying to condense it to half the number of lines like Denis suggested.  I was hoping to clarify a couple things if you guys don’t mind….

I wanna make sure I understand how this code is working.  So, from what I gather it first checks to see if the ‘guess’ is out of range and if that is false it continues to the next ‘if’ statement checking wether it’s too low.  Now this is where I’m not 100% sure if the too low ‘if’ statement is false does it skip everything that is nested below it (you are cold, warm, on fire) and go to the ‘if statement checking if it’s too high?   And now say the too low ‘if’ statement is true, because it’s an ‘if’ the code does not stop it continues but when it gets to the elif the code stops?  
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 - 10:
            print "You are cold!"
            print
            print "Sorry please try again."
            print
            print
        elif guess < secret - 5:
            print "You are warmer!"
            print
            print "Sorry please try again."
            print
            print
        else:
            print "You're on fire!!"
            print
            print "Sorry please try again."
            print
            print
    if guess > secret:
        print
        print "Too high!"
        if guess > secret + 10:
            print "You are cold!"
            print
            print "Sorry please try again."
            print
            print
        elif guess > secret + 5:
            print "You are warmer!"
            print
            print "Sorry please try again."
            print
            print
        else:
            print "You're on fire!!"
            print
            print "Sorry please try again."
            print
            print

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!"
    if guess < secret - 10 or guess > secret - 10:
        print "You are cold!"
        print
        print "Sorry please try again."
        print
        print
    elif guess < secret - 5 or guess > secret - 5:
        print "You are warmer!"
        print
        print "Sorry please try again."
        print
        print
    else:
        print "You're on fire!!"
        print
        print "Sorry please try again."
        print
        print
   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140310/5f3b0fee/attachment-0001.html>


More information about the Tutor mailing list