Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79005 > unrolled thread
| Started by | Shiva <shivaji_tn@yahoo.com> |
|---|---|
| First post | 2014-10-12 17:08 +0000 |
| Last post | 2014-10-12 19:35 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
while loop - multiple condition Shiva <shivaji_tn@yahoo.com> - 2014-10-12 17:08 +0000
Re: while loop - multiple condition Roy Smith <roy@panix.com> - 2014-10-12 13:28 -0400
Re: while loop - multiple condition Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-12 19:35 +0000
| From | Shiva <shivaji_tn@yahoo.com> |
|---|---|
| Date | 2014-10-12 17:08 +0000 |
| Subject | while loop - multiple condition |
| Message-ID | <mailman.14792.1413133694.18130.python-list@python.org> |
Why is the second part of while condition not being checked?
while ans.lower() != 'yes' or ans.lower()[0] != 'y':
ans = input('Do you like python?')
My intention is if either of the conditions are true the loop should break.
But the condition after 'or' doesn't seem to evaluate.
Thanks,
Shiva.
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-10-12 13:28 -0400 |
| Message-ID | <roy-5A8BE2.13280512102014@news.panix.com> |
| In reply to | #79005 |
In article <mailman.14792.1413133694.18130.python-list@python.org>,
Shiva <shivaji_tn@yahoo.com> wrote:
> Why is the second part of while condition not being checked?
>
> while ans.lower() != 'yes' or ans.lower()[0] != 'y':
> ans = input('Do you like python?')
>
>
> My intention is if either of the conditions are true the loop should break.
> But the condition after 'or' doesn't seem to evaluate.
A general rule of programming (be it Python or any other language) is to
break up complicated code into smaller pieces which can be understood
and tested in isolation. Your 'while' condition is kind of hairy.
There's a couple of complicated expressions, a logical conjuction, and
two different negated comparisons. That's a lot to understand all at
once.
I would factor that out into a little function:
-------------------------------------------------------
def is_yes(answer):
if answer.lower() == 'yes':
return True
if answer.lower()[0] == 'y':
return True
return False
while is_yes(ans):
ans = input('Do you like python?')
-------------------------------------------------------
Now, you can play around with your is_yes() function in an interactive
session and see what it returns for various inputs, in isolation from
the rest of your program:
print is_yes('y')
print is_yes('yes')
print is_yes('no')
print is_yes('you bet your sweet bippy')
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-10-12 19:35 +0000 |
| Message-ID | <m1el67$qr1$4@dont-email.me> |
| In reply to | #79005 |
On Sun, 12 Oct 2014 17:08:00 +0000, Shiva wrote: > while ans.lower() != 'yes' or ans.lower()[0] != 'y': while ans.lower() is not equal to "yes" or ans.lower()[0] is not equal to "y" the loop will continue to run Note that if ans.lower() == 'y', then the first clause ( ans.lower() != 'yes' ) is true, so the loop will continue to run, ignoring the result of the second clause ( ans.lower()[0] != 'y' ), This will also be the case if you reverse the order of the clauses. It seems that you need a better understanding of combinatorial logic, perhaps http://www.ee.surrey.ac.uk/Projects/Labview/boolalgebra/ index.html#booleantheorems will help. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web