Find the answer to your Linux question:
Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By unlimitedscolobb
Hi! Check out the snippet below: Code: print "\nEnter names, \"Q\" to quit: string = raw_input() if string == "Q" or string == "q": print "Exiting..." sys.exit(0) while string != ...
  1. #1
    HMW
    HMW is offline
    Just Joined!
    Join Date
    Nov 2010
    Location
    08 - Stockholm
    Posts
    19

    Python - and/or confusion!

    Hi!

    Check out the snippet below:

    Code:
    print "\nEnter names, \"Q\" to quit: 
    
    string = raw_input()
    
    if string == "Q" or string == "q":
    	print "Exiting..."
    	sys.exit(0)
    
    while string != "Q" and string != "q":
    	vector3.append(string)
    	string = raw_input()
    As you all can see, in the if-sequence I have an 'or', and it works that way. However; in the while-sequence I have an 'and', otherwise it does NOT work - I am truly confused by this and would appreciate any input.

    Thanks for reading,

    HMW

  2. #2
    Linux Guru
    Join Date
    May 2011
    Posts
    1,842
    Hey,

    Your code works for me. Well, I had to add a double-quote at the end of your first line to close the print statement, and comment out vectore3 call b/c I don't have that defined, but the and/or logic worked.

    Maybe post your whole code, and show what error statements python is reporting.

  3. #3
    Just Joined!
    Join Date
    Nov 2006
    Location
    near Berea, Kentucky (in a tipi)
    Posts
    34

    and/or confusion

    I am not certain I understand what you are asking. This looks to me like a problem in (positive/negative) Boolean logic:

    if (A or B)
    then exit.

    while (!A and !B)
    then do something.

    in effect:

    if A or B are true, both conditionals cause the program to end. Or, to put it another way, if both A and B are false, something gets done.

    What have I missed?

    And why are both tests necessary?

  4. #4
    Just Joined!
    Join Date
    Dec 2009
    Posts
    6
    I think what might be puzzling is the lack of symmetry: why must one use 'or' in one case and 'and' in the other, when both are there to exit the program?

    The answer is, the first 'if' exits when the condition is true, where as the while loop does the opposite, it keep going while it's condition is true.

    You might change your program to:

    Code:
    print "\nEnter names, \"Q\" to quit: 
    
    string = raw_input()
    
    if string == "Q" or string == "q":
    	print "Exiting..."
    	sys.exit(0)
    
    while True:
    	vector3.append(string)
    	string = raw_input()
            if string == "Q" or string == "q":
    	        print "Exiting..."
    	        sys.exit(0)
    You could also simplify:

    Code:
    print "\nEnter names, \"Q\" to quit: 
    
    while True:
    	string = raw_input()
            if string == "Q" or string == "q":
    	        print "Exiting..."
    	        sys.exit(0)
    	vector3.append(string)

    If this didn't help, please be more specific about your problem.

  5. #5
    Linux Newbie unlimitedscolobb's Avatar
    Join Date
    Jan 2008
    Posts
    120
    Check this out: De Morgan's laws - Wikipedia, the free encyclopedia

    The essential snippet:

    Code:
    NOT (P AND Q) = (NOT P) OR (NOT Q)
    NOT (P OR Q) = (NOT P) AND (NOT Q
    In your case P is string == "Q" and Q is string == "q".

  6. #6
    HMW
    HMW is offline
    Just Joined!
    Join Date
    Nov 2010
    Location
    08 - Stockholm
    Posts
    19
    Thank you all for your replies. I realize I should have been more specific, you are all great programmers - not mind readers!

    The snippet was to illustrate my confusion with "and" versus "or". The code itself is merely me trying to learn Python. I come from a C++ background, so I am simply writing some test programs with different stuff (vectors, if, while, for... you name it) to get the hang of things. I do remember being just as confused with "&&" versus "||" in C++. Maybe this is something I will simply have to learn to live with!

    The upside of me being thick with regards to this is that there is a 50% chance. If it's not behaving corectly the first time around, change "and" to "or" or vice versa.

    Thank you all again for your replies. This is how I educate myself; try - question - read - try again - learn. So all your input really matters to me.

    Thanks for reading this,

    HMW

  7. #7
    Linux Newbie unlimitedscolobb's Avatar
    Join Date
    Jan 2008
    Posts
    120
    Quote Originally Posted by HMW View Post
    The upside of me being thick with regards to this is that there is a 50% chance. If it's not behaving corectly the first time around, change "and" to "or" or vice versa.
    I still insist on you reading this Wikipedia article: De Morgan's laws - Wikipedia, the free encyclopedia . It is most surely going to help you out of the situation you are describing. It's okay if you don't understand everything immediately; just come back and ask more questions; we are here to help.

    I remember myself being confused with the and/or stuff; with time you the confusion just disappears (at least in my case it did)

    An easy approach to setting up simple conditions would be as follows: formulate the condition in natural language. If you have a "neither ... nor" construction, use and; if you have an "either ... or" construction, use or.

    For example, your first condition is "break when the user inputs either Q or q", so it's or. Your second condition is, on the other hand, "run until the input is neither Q, nor q", so it's and.
    elija likes this.

  8. #8
    HMW
    HMW is offline
    Just Joined!
    Join Date
    Nov 2010
    Location
    08 - Stockholm
    Posts
    19
    Quote Originally Posted by unlimitedscolobb View Post
    I still insist on you reading this Wikipedia article: De Morgan's laws - Wikipedia, the free encyclopedia . It is most surely going to help you out of the situation you are describing. It's okay if you don't understand everything immediately; just come back and ask more questions; we are here to help.

    I remember myself being confused with the and/or stuff; with time you the confusion just disappears (at least in my case it did)

    An easy approach to setting up simple conditions would be as follows: formulate the condition in natural language. If you have a "neither ... nor" construction, use and; if you have an "either ... or" construction, use or.

    For example, your first condition is "break when the user inputs either Q or q", so it's or. Your second condition is, on the other hand, "run until the input is neither Q, nor q", so it's and.
    Thanks mate. I will definitely check out the link. What you wrote above has already helped me a lot.

    Truly appreciate your input,

    HMW

  9. #9
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Another way of looking at this is to compare "=" and "!=" logic.

    "=" logic is very specific: that one value is true, everything else is false. "= or =" is a bit more general, but there is still a reasonable chance of the condition being true or false.

    "!=" logic is quite unspecific: any value except that one will be true. And with "!= or !=", the result is guaranteed to be true for all values, including the two specified.

    Specifically: (string != "Q") or (string != "q") is true for "Q" (the second condition is true), for "q" (the first condition is true) or anything else (both conditions are true).

    A condition that is always true doesn't work.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...