Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott W Dunning

On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:

GOT IT!!  Finally!  Thanks for all of your help!!

This is what I got, not sure if it’s correct but it’s working!

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 "Please play again!"
elif guess < secret - 5:
print "You are warmer!"
print
print "Please play again"
else:
print "You're on fire!!"
print
print "Please play again"
if guess > secret:
print
print "Too high!"
if guess > secret + 10:
print "You are cold!"
print
print "Please play again!"
elif guess > secret + 5:
print "You are warmer!"
print
print "Please play again"
else:
print "You're on fire!!"
print
print "Please play again"

   

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Alan Gauld

On 08/03/14 01:23, Scott W Dunning wrote:


On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:

GOT IT!!  Finally!  Thanks for all of your help!!

This is what I got, not sure if it’s correct but it’s working!


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.



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 "Please play again!"
 elif guess < secret - 5:
 print "You are warmer!"
 print
 print "Please play again"
 else:
 print "You're on fire!!"
 print
 print "Please play again"
 if guess > secret:
 print
 print "Too high!"
 if guess > secret + 10:
 print "You are cold!"
 print
 print "Please play again!"
 elif guess > secret + 5:
 print "You are warmer!"
 print
 print "Please play again"
 else:
 print "You're on fire!!"
 print
 print "Please play again"



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

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread spir

On 03/08/2014 10:13 AM, Alan Gauld wrote:

On 08/03/14 01:23, Scott W Dunning wrote:


On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:

GOT IT!!  Finally!  Thanks for all of your help!!

This is what I got, not sure if it’s correct but it’s working!


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.


Yes, and note the relevant piece of data is the absolute value: 
abs(secret-guess). This gives you at once on-fire / hot / warm / cold / icy ... 
whatever you like ;-) (pretty sexy game, guess-my-number!).


d
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Mark Lawrence

On 08/03/2014 01:23, Scott W Dunning wrote:


On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:

GOT IT!!  Finally!  Thanks for all of your help!!


If at first you don't succeed... :)



This is what I got, not sure if it’s correct but it’s working!

def print_hints(secret, guess):
 if guess < 1 or guess > 100:


Only now do I feel that it's time to point out that the above line would 
probably be written by an experienced Python programmer as:-


if 1 > guess > 100:

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Dave Angel
 Mark Lawrence  Wrote in message:
> On 08/03/2014 01:23, Scott W Dunning wrote:
>
>>
>> def print_hints(secret, guess):
>>  if guess < 1 or guess > 100:
> 
> Only now do I feel that it's time to point out that the above line would 
> probably be written by an experienced Python programmer as:-
> 
> if 1 > guess > 100:
> 

With an appropriate 'not' or its equivalent,  of course. 


-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread eryksun
On Sat, Mar 8, 2014 at 8:36 AM, Dave Angel  wrote:
>  Mark Lawrence  Wrote in message:
>> On 08/03/2014 01:23, Scott W Dunning wrote:
>>
>>> def print_hints(secret, guess):
>>>  if guess < 1 or guess > 100:
>>
>> Only now do I feel that it's time to point out that the above line would
>> probably be written by an experienced Python programmer as:-
>>
>> if 1 > guess > 100:
>>
>
> With an appropriate 'not' or its equivalent,  of course.

i.e.

guess < 1 or guess > 100

becomes

not not (guess < 1 or guess > 100)

distribute over the disjunction

not (not (guess < 1) and not (guess > 100))

logically negate the comparisons

not (1 <= guess and guess <= 100)

finally, write the conjoined comparisons as a chained comparison:

not (1 <= guess <= 100)

i.e., guess isn't in the closed interval [1, 100].

Anyway, you needn't go out of your way to rewrite the expression using
a chained comparison. The disjunctive expression is actually
implemented more efficiently by CPython's compiler, which you can
verify using the dis module to disassemble the bytecode.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Mark Lawrence

On 08/03/2014 14:29, eryksun wrote:


Anyway, you needn't go out of your way to rewrite the expression using
a chained comparison. The disjunctive expression is actually
implemented more efficiently by CPython's compiler, which you can
verify using the dis module to disassemble the bytecode.


I have no interest in the efficiency, only what is easiest for me to 
read, which in this case is the chained comparison.  As a rule of thumb 
I'd also prefer it to be logically correct :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 6:36 AM, Dave Angel  wrote:
> 
> Mark Lawrence  Wrote in message:
>>> On 08/03/2014 01:23, Scott W Dunning wrote:
>>> 
>>> 
>>> def print_hints(secret, guess):
>>> if guess < 1 or guess > 100:
>> 
>> Only now do I feel that it's time to point out that the above line would 
>> probably be written by an experienced Python programmer as:-
>> 
>> if 1 > guess > 100:
> 
> With an appropriate 'not' or its equivalent,  of course. 
> 
This is how our teacher wanted it all written under the print_hints function.  
Besides if I was an experienced python programmer I wouldn't be asking such 
trivial stuff under the tutor forum lol. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 6:26 AM, Mark Lawrence  wrote:
> 
>> On 08/03/2014 01:23, Scott W Dunning wrote:
>> 
>> On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:
>> 
>> GOT IT!!  Finally!  Thanks for all of your help!!
> 
> If at first you don't succeed... :)
> 
>> 
>> This is what I got, not sure if it’s correct but it’s working!
>> 
>> def print_hints(secret, guess):
>> if guess < 1 or guess > 100:
> 
> Only now do I feel that it's time to point out that the above line would 
> probably be written by an experienced Python programmer as:-
> 
> if 1 > guess > 100:
> 
OH!  I see what you're saying, ignore my last post.  Yes that looks cleaner.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread Scott dunning

> On Mar 8, 2014, at 3:57 AM, spir  wrote:
> 
>> On 03/08/2014 10:13 AM, Alan Gauld wrote:
>>> On 08/03/14 01:23, Scott W Dunning wrote:
>>> 
>>> On Mar 7, 2014, at 11:02 AM, Alan Gauld  wrote:
>>> 
>>> GOT IT!!  Finally!  Thanks for all of your help!!
>>> 
>>> This is what I got, not sure if it’s correct but it’s working!
>> 
>> 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.
Yeah I'm gonna try that.  The reason I split it up in the first place is I 
couldn't get it to work properly being all together (ie Either too high or too 
low yet always either cold, warm, or on fire).

> 
> Yes, and note the relevant piece of data is the absolute value: 
> abs(secret-guess). This gives you at once on-fire / hot / warm / cold / icy 
> ... whatever you like ;-) (pretty sexy game, guess-my-number!).
> 
Hmm, not sure I understand.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with Guess the number script

2014-03-08 Thread eryksun
On Sat, Mar 8, 2014 at 1:44 PM, Scott dunning  wrote:
>> if 1 > guess > 100:
>>
> OH!  I see what you're saying, ignore my last post.  Yes that looks
> cleaner.

Please read section 6.9 of the language reference, which defines
Python comparison expressions.

http://docs.python.org/3/reference/expressions#not-in

Here's the description of chained comparisons:

Comparisons can be chained arbitrarily, e.g.,
x < y <= z is equivalent to x < y and y <= z,
except that y is evaluated only once (but in
both cases z is not evaluated at all when
x < y is found to be false).

Formally, if a, b, c, ..., y, z are expressions
and op1, op2, ..., opN are comparison operators,
then a op1 b op2 c ... y opN z is equivalent to
a op1 b and b op2 c and ... y opN z, except
that each expression is evaluated at most once.

Note that a op1 b op2 c doesn’t imply any kind
of comparison between a and c, so that, e.g.,
x < y > z is perfectly legal (though perhaps
not pretty).

Thus `1 > guess > 100` is equivalent to `(guess < 1) and (guess >
100)`, which is always false. The correct chained comparison is `not
(1 <= guess <= 100)`.

Chaining is generally simpler, since all expressions are only
evaluated once. In this particular case, with a local variable
compared to constants, the chained form is slightly less efficient in
CPython. Though if it "looks cleaner" to you, certainly use it.
Readability takes precedence.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor