Re: [Tutor] 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why isn't my simple if elif script not working?
On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano st...@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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Better how? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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 st...@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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why isn't my simple if elif script not working?
On Wed, Jul 18, 2012 at 7:44 AM, Steven D'Aprano st...@pearwood.info wrote: Alexandre Zani wrote: On Wed, Jul 18, 2012 at 6:09 AM, Steven D'Aprano st...@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! Thanks. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] 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@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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 Cleese, then I believe it prints Michael Palin while reporting True, so satisfying the elif clause. Cheers, Andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why isn't my simple if elif script not working?
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
Re: [Tutor] Why isn't my simple if elif script not working?
On Tue, Jul 17, 2012 at 10:21 PM, Andre' Walker-Loud walksl...@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) True Michael Palin == (John Cleese or Michael Palin) False (John Cleese or Michael Palin) 'John Cleese' Python will look at the expression (John Cleese or Michael Palin) since bool(John Cleese) is True, the expression immediately evaluates to John Cleese and the elif clause becomes equivalent to name == John Cleese 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 Cleese, then I believe it prints Michael Palin while reporting True, so satisfying the elif clause. Cheers, Andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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 walksl...@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) True Michael Palin == (John Cleese or Michael Palin) False (John Cleese or Michael Palin) 'John Cleese' Python will look at the expression (John Cleese or Michael Palin) since bool(John Cleese) is True, the expression immediately evaluates to John Cleese and the elif clause becomes equivalent to name == John Cleese thanks - I fell into the same trap! I tried it in my terminal but it worked for the same reason as the OP. Andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 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@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor