By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,031 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Confused about while statement

P: n/a
EAS
In theory, the following code should ask for the user to enter a value for h
until he/she enters hello or goodbye.

h = "hi"
while h != "hello" or "goodbye":
h = raw_input("Value for h:")

But the program keeps asking for a value no matter what I enter. Why doesn't
it work?
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
EAS wrote:
In theory, the following code should ask for the user to enter a value
for h
until he/she enters hello or goodbye.

h = "hi"
while h != "hello" or "goodbye":
h = raw_input("Value for h:")

But the program keeps asking for a value no matter what I enter. Why
doesn't
it work?


You meant

while h != "hello" and h != "goodbye": ...

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ She glanced at her watch ... It was 9:23.
-- James Clavell
Jul 18 '05 #2

P: n/a
In article <40***************@alcyone.com>,
Erik Max Francis <ma*@alcyone.com> wrote:
EAS wrote:
In theory, the following code should ask for the user to enter a value
for h
until he/she enters hello or goodbye.

h = "hi"
while h != "hello" or "goodbye":
h = raw_input("Value for h:")

But the program keeps asking for a value no matter what I enter. Why
doesn't
it work?


You meant

while h != "hello" and h != "goodbye": ...


Or, perhaps even better,

while h not in ("hello", "goodbye"):

The meaning is the same, but I think idiomatically, it's a closer match
to the way you would say it in natural language.
Jul 18 '05 #3

P: n/a

"EAS" <er****@attbi.nospam.com> wrote in message
news:dHcrc.86491$536.14466932@attbi_s03...
In theory, the following code should ask for the user to enter a value for h until he/she enters hello or goodbye.

h = "hi"
while h != "hello" or "goodbye":
h = raw_input("Value for h:")

But the program keeps asking for a value no matter what I enter. Why doesn't it work?
Operator precedence and a misunderstanding about how "or" works.

Operator precedence means that the expression is equivalent to:

(h != "hello") or "goodbye"

The result of h != "hello" is either True or False.
The way "or" works, if the result was false, the
second operand would be substituted, so you
would get "goodbye" which is true. In other words,
the result of the entire expression is either True
or "goodbye", which is also true. So the loop never terminates.

The "proper" way to write this test is:

while h not in ("hello", "goodbye"):

There are other ways, but this is probably the most readable.

John Roth


Jul 18 '05 #4

P: n/a
h != "hello" or "goodbye"
is
(h != "hello") or "goodbye"

It's always true because even if (h != "hello") evaluates to False,
then "goodbye" is 'tested' and considered to be true.

You probably meant:
h != "hello" and h != "goodbye"
that can be written even better as:
h not in ("hello", "goodbye")

Georgy

"EAS" <er****@attbi.nospam.com> wrote in message news:dHcrc.86491$536.14466932@attbi_s03...
| In theory, the following code should ask for the user to enter a value for h
| until he/she enters hello or goodbye.
|
| h = "hi"
| while h != "hello" or "goodbye":
| h = raw_input("Value for h:")
|
| But the program keeps asking for a value no matter what I enter. Why doesn't
| it work?
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.