944,104 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2630
  • Python RSS
Dec 27th, 2005
0

A simple question on operator

Expand Post »
I can do a control loop like this in C++

Python Syntax (Toggle Plain Text)
  1. int case =0;
  2. 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
Python Syntax (Toggle Plain Text)
  1. while case<0 & case>5:
Similar Threads
Reputation Points: 13
Solved Threads: 0
Light Poster
k_en is offline Offline
35 posts
since Jul 2005
Dec 27th, 2005
0

Re: A simple question on operator

Hi!

So you want to do something if case is smaller than 0 or greater than 20, right?
Python Syntax (Toggle Plain Text)
  1. while case<0 or case>20: do something
If case should be between 0 and 20:
Python Syntax (Toggle Plain Text)
  1. while 0<case<20: do something
Hope this helps.

Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Dec 27th, 2005
0

Re: A simple question on operator

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


Python Syntax (Toggle Plain Text)
  1. while case<0 or case>20: do something
Reputation Points: 13
Solved Threads: 0
Light Poster
k_en is offline Offline
35 posts
since Jul 2005
Dec 27th, 2005
0

Re: A simple question on operator

Something like this works fine:
Python Syntax (Toggle Plain Text)
  1. try:
  2. case = int(raw_input("enter a number less then 3 or more than 4: "))
  3. except:
  4. case = 0
  5. while case <=2 or case >=5:
  6. # do something to get out of loop
  7. case += 1
  8. print case
  9. if case > 10: break
Notice that case is reserved keyword in C++, also notice that && is and || is or in Python. A simple & is bitwise and in C++ and Python.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: New to python
Next Thread in Python Forum Timeline: Modifying a project in python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC