[Tutor] How to handle conjunction operators

Timo timomlists at gmail.com
Mon Nov 28 12:18:49 CET 2011


Op 27-11-11 20:02, Hugo Arts schreef:
> On Sun, Nov 27, 2011 at 7:52 PM, surya k<suryak at live.com>  wrote:
>> Hi,
>> Could you please tell me why this isn't working and how can I make it
>> possible...
>> Consider this code..
>>
>> name = raw_input("Enter your first name: ")
>> if name[0] == ("m" or "f" or "b") :
>>     rhyme = name[1:]
>>
>> What I want here is.. If the name starts with 'm' or 'f' or 'b', The first
>> letter should be removed.
>> But this isn't happening here.
> This is a very common error. Fact is, the or operator just isn't
> distributive with respect to the == operator like that, and for good
> reason. What you want is either this:
>
> if name[0] == "m" or name[0] == "f" or name[0] == "b":
>
> or, more idiomatically, you can use the in operator like this:
>
> if name[0] in ("m", "f", "b"):
Or even shorter:
if name[0] in "mfb":

Cheers,
Timo

>
> HTH,
> Hugo
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list