#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Confusion with and/or


    I appear to have a mental block on the use of and/or.
    In the following statement let's pretend that
    llower_bound = 1 and lupper_bound = 10
    c = 5
    if ord(c) >= llower_bound or c <= ulower_bound:
    It seems to me that this statement should use an 'and' statement not an 'or' statement.
    The purpose is to test if both criteria are correct.
    if c = 25, then ord(c) >= llower_bound would be correct but not for c <= ulower_bound.

    This is the type of question I would expect the question, "Is this homework."
    Yes, for a self study course. The program is written, working and below.
    I just can't seem to get through my head why 'or' works and 'and' does not.
    Hopefully I did my code tags right. Script looks like a pile in this window.

    Thank you,
    Jason

    Code:
    llower_bound = ord('a')
    ulower_bound = ord('z')
    lupper_bound = ord('A')
    uupper_bound = ord('Z')
    coded_phrase = ''
    
    #prompt for the message to be coded and oddset key.
    phrase = raw_input('Please enter a phrase. ')
    offset = input('Please enter a cypher key ')
    
    #Create for loop to process each char in phrase.
    for c in phrase:
    
        #If loop to test if the char is upper or lower case.
        #Process 'if' if the letter is lower.
        #Process 'elif' if the letter is upper.
        #Process 'else' for any other char.
        if ord(c) >= llower_bound or c <= ulower_bound:
            #convert c to ASCII and assign to conv.
            conv = ord(c)
            #convert conv to the offset value.
            #subtract the llower_bound for lower case and add the offset.
            #mod the result to and add the llower_bound back to the value.
            #convert back to chr and assign to coded.
            #append the converted c (now called coded) to the end of coded_phrase.
            coded = chr((conv - llower_bound + offset)%26 + llower_bound)
            coded_phrase = coded_phrase + coded
            #repeat for upper case using lupper_bound.
        elif ord(c) >= lupper_bound or c <= uupper_bound:
            conv = ord(c)
            coded = chr((conv - lupper_bound + offset)%26 + lupper_bound)
            coded_phrase = coded_phrase + coded
        else:
            #do not convert any other chr's.
            coded_phrase = coded_phrase + c
    
    #print the coded message.        
    print coded_phrase
    Last edited by Greycon; August 3rd, 2012 at 12:17 PM. Reason: Fixed my code tags.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    83
    Rep Power
    39
    The first thing to note is that "c <= ulower_bound" and "c <= llower_bound" are always false because c is a string and the bound variables are integers and in Python 2 "string <= integer" always returns false (in Python 3 it would give you an error, which would have actually been much more helpful in explaining the behavior you're seeing).

    So the reason that "and" didn't work for you is that both the conditions boil down to "something and False" and that's always False. By using "or" you end up with "something or False" which is equivalent to just "something". So your if-statements are effectively equivalent to this:

    Code:
    if ord(c) >= llower_bound:
        # handle lower case
    elif ord(c) >= lupper_bound:
        # handle upper case
    else:
        # handle rest
    The reason that this doesn't totally blow up is that upper case letters come first in the ASCII table. So when you enter an upper case letter the if condition will be false and the elif condition will be true, so it correctly goes in the elif branch. Likewise if you enter a lower case letter, it will go in the if-branch because the if condition is true and if you enter a character that comes before any letters in the ASCII table, it will correctly go in the else-branch.

    However that doesn't mean the code is correct. If you enter something that comes between upper and lower case letters in the ASCII table (e.g. the underscore "_"), it will be converted to an upper case letter, and if you enter something that comes after lower case letters in the ASCII table (e.g. the pipe "|"), it will end up as a lower case letter.

    So your first instinct was right: "and" is right and "or" is wrong - it just seemed to work better with "or" because you had a mistake in your conditions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0
    sepp2k1,

    Wow, thank you.
    Not sure how I missed that.
    Ok, modified it to Ord(c) and it's working as I would expect with an 'and'.

    Thank you for pointing that out.
    I was more than a little confused as to why it was taking 'or' and not 'and'.

    Jason

IMN logo majestic logo threadwatch logo seochat tools logo