Santosh Kumar | 18 Jul 2012 07:09
Picon
Gravatar

Why isn't my simple if elif script not working?

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 <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Andre' Walker-Loud | 18 Jul 2012 07:21
Picon

Re: Why isn't my simple if elif script not working?

Hi Santosh,

On Jul 17, 2012, at 10:09 PM, Santosh Kumar 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.

you just have to be careful with the multiple boolean line in the elif.  You can use either

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

or

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

With your elif line, it is asking "does name ==  John Cleese" or "Michael Palin", and so if the name is not John
(Continue reading)

Alexandre Zani | 18 Jul 2012 07:29
Picon

Re: Why isn't my simple if elif script not working?

On Tue, Jul 17, 2012 at 10:21 PM, Andre' Walker-Loud
<walksloud <at> gmail.com> wrote:
> Hi Santosh,
>
> On Jul 17, 2012, at 10:09 PM, Santosh Kumar 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.
>
> you just have to be careful with the multiple boolean line in the elif.  You can use either
>
> elif name == ("John Cleese" or "Michael Palin"):

That won't work.

>>> "John Cleese" == ("John Cleese" or "Michael Palin")
(Continue reading)

Andre' Walker-Loud | 18 Jul 2012 07:31
Picon

Re: Why isn't my simple if elif script not working?

> 
> On Jul 17, 2012, at 10:29 PM, Alexandre Zani wrote:
> 
> 
> On Tue, Jul 17, 2012 at 10:21 PM, Andre' Walker-Loud
> <walksloud <at> gmail.com> wrote:
>> Hi Santosh,
>> 
>> On Jul 17, 2012, at 10:09 PM, Santosh Kumar 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.
>> 
>> you just have to be careful with the multiple boolean line in the elif.  You can use either
>> 
>> elif name == ("John Cleese" or "Michael Palin"):
(Continue reading)

Alexandre Zani | 18 Jul 2012 07:23
Picon

Re: Why isn't my simple if elif script not working?

On Tue, Jul 17, 2012 at 10:09 PM, Santosh Kumar <sntshkmr60 <at> 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 <at> 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
(Continue reading)

Steven D'Aprano | 18 Jul 2012 15:09

Re: Why isn't my simple if elif script not working?

Alexandre Zani wrote:

> What you want to write is this:
> 
> elif name == "John Cleese" or name == "Michael Palin":

elif name in ("John Cleese", "Michael Palin"):

is better.

--

-- 
Steven

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

Alexandre Zani | 18 Jul 2012 16:34
Picon

Re: Why isn't my simple if elif script not working?

On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano <steve <at> pearwood.info> wrote:
> Alexandre Zani wrote:
>
>> What you want to write is this:
>>
>> elif name == "John Cleese" or name == "Michael Palin":
>
>
> elif name in ("John Cleese", "Michael Palin"):
>
> is better.
>
>
> --
> Steven
>
>
> _______________________________________________
> Tutor maillist  -  Tutor <at> python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Better how?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Steven D'Aprano | 18 Jul 2012 16:44

Re: Why isn't my simple if elif script not working?

Alexandre Zani wrote:
> On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano <steve <at> pearwood.info> wrote:
>> Alexandre Zani wrote:
>>
>>> What you want to write is this:
>>>
>>> elif name == "John Cleese" or name == "Michael Palin":
>>
>> elif name in ("John Cleese", "Michael Palin"):
>>
>> is better.

> Better how?

It's shorter, you don't have to repeat the reference to `name` twice, it is 
more easily extensible if you add additional names, and it is likely to be a 
*tiny* bit faster -- it moves the equality test out of pure-Python code into a 
tuple method, which will be written in C.

--

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

Alexandre Zani | 18 Jul 2012 19:01
Picon

Re: Why isn't my simple if elif script not working?

On Wed, Jul 18, 2012 at 7:44 AM, Steven D'Aprano <steve <at> pearwood.info> wrote:
> Alexandre Zani wrote:
>>
>> On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano <steve <at> pearwood.info>
>> wrote:
>>>
>>> Alexandre Zani wrote:
>>>
>>>> What you want to write is this:
>>>>
>>>> elif name == "John Cleese" or name == "Michael Palin":
>>>
>>>
>>> elif name in ("John Cleese", "Michael Palin"):
>>>
>>> is better.
>
>
>> Better how?
>
>
>
> It's shorter, you don't have to repeat the reference to `name` twice, it is
> more easily extensible if you add additional names, and it is likely to be a
> *tiny* bit faster -- it moves the equality test out of pure-Python code into
> a tuple method, which will be written in C.
>

Wadayano... You're right, it is faster!

(Continue reading)

Santosh Kumar | 22 Jul 2012 07:58
Picon
Gravatar

Re: Why isn't my simple if elif script not working?

In first half of this script:

prompt = raw_input("Can you tell me your name? ")
if prompt in ("Yes", "yes", "y", "Y"):
    name = raw_input("What's your name? ")
elif prompt in ("No", "no", "n", "N"):
    exit("OK, have nice day..")
else:
    prompt = name

if name == "Santosh":
    print "Hey! I have the same name."
elif name in ("John Cleese", "Michael Palin"):
    print "I have no preference about your name. Really!!"
else:
    print "You have a nice name."

I want that if anyone enter their name at the first prompt the name
should be stored in "name" variable, or else if they enter anyone of
("Yes", "yes", "y", "Y") they should be given another prompt where
they can enter their name. But currently when I enter name other than
("Yes", "yes", "y", "Y") it says that name is not defined. How can I
fix this?
_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Alan Gauld | 22 Jul 2012 10:35

Re: Why isn't my simple if elif script not working?

On 22/07/12 06:58, Santosh Kumar wrote:

> prompt = raw_input("Can you tell me your name? ")
> if prompt in ("Yes", "yes", "y", "Y"):
>      name = raw_input("What's your name? ")
> elif prompt in ("No", "no", "n", "N"):
>      exit("OK, have nice day..")
> else:
>      prompt = name

Don't you want

        name = prompt?

> I want that if anyone enter their name at the first prompt the name
> should be stored in "name" variable, or else if they enter anyone of
> ("Yes", "yes", "y", "Y") they should be given another prompt where
> they can enter their name. But currently when I enter name other than
> ("Yes", "yes", "y", "Y") it says that name is not defined. How can I
> fix this?

--

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

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
(Continue reading)

Ross Wilson | 18 Jul 2012 07:54
Picon

Re: Why isn't my simple if elif script not working?


On 18/07/12 15:09, Santosh Kumar 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 <at> python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Note that you *can* do:

     if name in ("John Cleese", "Michael Palin"):

Ross
_______________________________________________
(Continue reading)


Gmane