954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A simple question on operator

I can do a control loop like this in C++

int case =0;
 while( case<0 && case>20){}


How am i suppose to implement this in Python? I didn't get any errors if i wrote this, the problem is it doesn't enter the while loop when case<0 and case>5

while case<0 & case>5:
k_en
Light Poster
35 posts since Jul 2005
Reputation Points: 13
Solved Threads: 0
 

Hi!

So you want to do something if case is smaller than 0 or greater than 20, right?

while case<0 or case>20: do something

If case should be between 0 and 20:

while 0<case<20: do something

Hope this helps.

Regards, mawe

mawe
Junior Poster
133 posts since Sep 2005
Reputation Points: 19
Solved Threads: 58
 

If case <=2 or case >=5, it will enter the while loop. the code below doesn't solve my problem. why can't it enter the while loop when my case match this condition case <=2 or case >=5

while case<0 or case>20: do something
k_en
Light Poster
35 posts since Jul 2005
Reputation Points: 13
Solved Threads: 0
 

Something like this works fine:

try:
    case = int(raw_input("enter a number less then 3 or more than 4: "))
except:
    case = 0
while case <=2 or case >=5:
    # do something to get out of loop
    case += 1
    print case
    if case > 10: break

Notice that case is reserved keyword in C++, also notice that && isand || is or in Python. A simple & is bitwise and in C++ and Python.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You