[Tutor] binary - denary converter (was Additional help)

bob gailer bgailer at gmail.com
Mon Feb 11 00:14:44 CET 2013


I am changing the subject line to one that is more explicit.

On 2/10/2013 3:29 PM, Ghadir Ghasemi wrote:
>      elif choice not in '1' or '2' or '3':
>          print("Invalid input-try again!")
This works, but not for the reasons you had in mind.

For experiment, try:

 >>> '1' or '2' or '3'
'1'

 >>> '3' or'2' or '1'
'3'

 >>> '1' not in '1' or '2' or '3'
'2'

It is easy to stare at this and say "huh"? or "that makes no sense". 
Instead read up on "in", "or" and "operator precedence" until you do 
understand it.

 From the Language Reference:

5:9 The operators in and not in test for collection membership. x in s 
evaluates to true if x is a member of the collection s

5:10 The expression x or y first evaluates x; if x is true, its value is 
returned; otherwise, y is evaluated and the resulting value is returned.

5:15 for operator precedence

Also realize that all you need at the end is:

     else:
         print("Invalid input-try again!")

For checking an item against several possibilities:

     elif choice not in ('1', '2', '3'):

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list