473,569 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Valu e 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 1204
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("Valu e 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.co m> 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("Valu e 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.n ospam.com> wrote in message
news:dHcrc.8649 1$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("Valu e for h:")

But the program keeps asking for a value no matter what I enter. Why doesn't it work?
Operator precedence and a misunderstandin g 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.n ospam.com> wrote in message news:dHcrc.8649 1$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("Valu e 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
4597
by: jfj | last post by:
Hi. Suppose this: ######################## def foo (x): print x f = classmethod (foo)
11
4924
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., http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Tidy7IDbDHA.2108%40cpmsftngxa06.phx.gbl) that indicate that ASP will queue up requests when they come...
11
1293
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 = 0; return text;
6
1808
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 */ main() { int c;
6
1532
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 following snippets. 1) { int i=3; for (i--; i<7; i=7) printf("%d",i++);
26
4667
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 Perl like so, and it's pretty to me (and the using of the heredocs I think does defend perl against many arguments withthe HTML being all escaped and...
4
1424
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 is the alias? class MyClass
15
2455
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; ofstream editFile; editFile.open (name, ios::out | ios::app);
2
8092
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; SELECT COUNT(*)INTO TOTAL_LEFT FROM DBSAMBA.REPORTEDTRANSACTION_S; WHILE (TOTAL_LEFT > 0) DO
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.