0

If I define:

def hasNoX_2(s):
    if type(s)!=str:
       return False
    for c in s:
        if c!='x' or c!='X':
           return True
    return False

and enter hasNoX_2('Xenon'), True is returned and I'm not sure why. Nothing is being returned in the first If statement, since it only returns False if s is NOT a string. So what is the for loop "looking" at when it says, "Hey, I don't see 'x' or 'X', so I can return True?"

5
  • because your logic tests if at least one letter is not x. Just run manually through your code and you'll see why. Commented Sep 13, 2017 at 0:28
  • it returns on first itteration, because evaluation of your if statement is True or False, which is obviously True Commented Sep 13, 2017 at 0:30
  • I've fixed the indentation Commented Sep 13, 2017 at 0:30
  • the code returns True if ANY character in the string is not an 'x' or 'X' Commented Sep 13, 2017 at 0:31
  • Actually, it will always return True - even for the string 'xxxxx'. the only string it will return False for is the empty string "" Commented Sep 13, 2017 at 0:34

4 Answers 4

2

Let's focus just on the if statement

if c!='x' or c!='X':

This is a boolean expression with two terms

  • c != 'x' and
  • c != 'X'

As you use the or operator, if either term evaluates as True, then the entire expression evaluates as True. If the expression evaluates as True, then the body of the if statement (in this case return True) is executed.

Here is the standard truth table for an or expression (A or B)

 A or B     | A = False | A = True |
 ----------------------------------
| B = False | False     | True     |
| B = True  | True      | True     |

As you can see, the result is False only if both terms are False

Let's look at how your expression evaluates for each type of input

c                   | c != 'x' | c != 'X' | or expression |
----------------------------------------------------------
'x'                 | False    | True     | True          |
'X'                 | True     | False    | True          |
any other character | True     | True     | True          |

In short, your expression will always evaluate as True - and the if branch will always be taken.

So, the function will return True when it examines the first character in the provided string.

The only ways this function can return False are

  • If something other than a str is provided
  • If an empty string is provided - because the for loop is never entered.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time to do this! This is very helpful.
1

I suppouse you trying to achieve True if all chars in input string not equal 'x' or 'X'.

If so, you could simply use the following code:

def hasNoX_2(s):

    if isinstance(s, str):
        return 'x' not in s.lower()

    return False

print(hasNoX_2('Xenon'))
print(hasNoX_2('eenon'))

1 Comment

This is valid if you have interpreted the OP's intent correctly. However, they are actually asking why their code always returns True. See my answer
1

So basically you could scan the characters of the string one at a time till you find 'x' or 'X', and return false. In case you reach the end of the loop iterating over the characters of the string, it would be a good opportunity to return True then, as no 'x' or 'X' was found anyways.

so your new code would look something like this:

    def hasNoX_2(s):
       if type(s)!=str:
           return False
       for c in s:
           if c=='x' or c=='X':
               return False
       return True

Comments

0

You iterate in your for loop through each character in the string. So it's looking at each character. Pretty much your loop is looking for when those if statement is true.

However using or if one of them is true the whole thing conditional evaluates to true so use and instead to make sure both statements are true

3 Comments

See my answer - the if statement evaluates as true for ALL characters.
Right I see now that person would most likely have to use and to get both cases
Probably .. but the function name is unhelpful. Does the function return True if there are "No X's in the string" or if there is "something other than X in the string"? That's why I just tried to focus on explaining the result they were seeing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.