473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Confused about while statement

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
4 1201
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: jfj | last post by:
Hi. Suppose this: ######################## def foo (x): print x f = classmethod (foo)
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
11
by: sathyashrayan | last post by:
Please look at the following code: static char *text = {"th", "st", "nd", "rd"}; char *ordinal_text(int number) { if (((number %= 100) > 9 && number < 20) || (number %= 10) > 3) number =...
6
by: m_a_t_t | last post by:
Ok, I'm reading "The C Programming Language: 2nd Edition" and I'm on chapter 1.5.1 and here's the program you're sposed to make: #include <stdio.h> /* copy input to output; 1st version */...
6
by: rahul8143 | last post by:
hello, I am really confused over following for code snippets. No problem that they are working codes but how? how they are evaluated by compiler? It will be my pleasure if you explain me all...
26
by: Dodger | last post by:
Okay, background... yes, I am another of those evil, spurned, damnable Perl mongers, but I'm not trying to start a flamewar, I'm juust tryung to understand something... I can write a script in...
4
by: vcinquini | last post by:
I've always learned about typedef as a statement that creates an alias for a type. For example: typedef long DWORD; but I'm confused about the following typedef. Which is the type and which...
15
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name;...
2
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.