[Tutor] Help with Guess the number script

spir denis.spir at gmail.com
Tue Mar 11 14:31:54 CET 2014


On 03/11/2014 05:07 AM, Scott W Dunning wrote:
>>> 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

I think here if the condition is true, you could just quit the function 
(return), no? The rest does not make much sense, I guess...
>      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


Below, the "temperature" hint and low/high hint are logically independant. The 
first one depends on the distance between secret and guess numbers, the second 
one depends on their relative values (greater/smaller). And the second hint 
(low/high) only makes sense iff the player did not win, meaning iff not "on fire!".

However, both are related to the difference. Conceptually, after having passed 
the test out-of-range, I would start with something like:

	diff = guess - secret
	
	# (we know guess is in range)
	# temperature hint
	dist = abs(diff)
	if dist == 0:
             ... on fire!
	    return
	elif dist < 5:
	    ...

	# (we know secret was not found)
	# high/low hint
	neg = diff < 0
	...

As an exercise, you could write each kind of hint in a separate tool func, and 
call each one only when relevant.

Denis


More information about the Tutor mailing list