g.z.
g.z.

Reputation: 143

python return within loop

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?"

Upvotes: 0

Views: 432

Answers (4)

Dhruv Mohan Datta
Dhruv Mohan Datta

Reputation: 11

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

Upvotes: 1

jamiroquai93
jamiroquai93

Reputation: 27

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

Upvotes: 0

kdopen
kdopen

Reputation: 8215

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.

Upvotes: 2

Yaroslav Surzhikov
Yaroslav Surzhikov

Reputation: 1608

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'))

Upvotes: 1

Related Questions