On Tue, Jul 17, 2012 at 10:09 PM, Santosh Kumar <sntshkm...@gmail.com> wrote:
> Here is my script:
>
> name = raw_input("What's your name? ")
>
> if name == "Santosh":
>     print "Hey!! I have the same name."
> elif name == "John Cleese" or "Michael Palin":
>     print "I have no preference about your name. Really!!"
> else:
>     print "You have a nice name."
>
>
> The if part works well. The elif part works well too. The problem is
> even if you enter strings other than "Santosh", "John Cleese" and
> "Michael Palin" you still get the print from elif part, not from else
> part.
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Think of it this way: The or operator doesn't look at both sides at
the same time. It looks at one side, and then at the other. So on one
side it looks and sees (name == "John Cleese") and decides whether
that is true or not. Then it looks at the other side and sees
("Michael Palin") which is always true because a non-empty string is
always true. It doesn't see "Michael Palin" in the context of name ==
"John Cleese". It sees and treats "Michael Palin" independently. So
because "Michael Palin" is always True, the elif clause is always
true.

What you want to write is this:

elif name == "John Cleese" or name == "Michael Palin":

Alex
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to