I'm testing code and I"m not sure what the issue is

The purpose of this code is to see whether or not there are an equal number of 'x’s and 'o’s.

def xo(s):
    if s.count('x') == s.count('o'):

        return True
    else:
        return False

It passed most of the tests but failed 4:

xo(roxopooooUtLooxhdoxxxQoxNoxCxokoxooxxGxASxfxEooooJbmxxxxx) should be True
xo(xxxxxooxxoooxbooxxxoxoxoxxxoxxxoxoxoooxxoxxAxooooo) should be False
xo(JhkxxxbGxdQxxxpxAxxooxrxmNooLofxxCooxExo) should be False

And I guess my function is or is not case sensitive. I can’t tell

It shouldn’t need to be case sensitive since all the tests use x and o.

If you did want to make it case insensitive,

def xo(s):
    s = s.lower()
    # ...

If I run your code with the test cases you’ve posted, I get the results you’re expecting…

Not sure why you aren’t.

It’s case sensitive, unless you change it how @mtf has demonstrated. :slight_smile:

>>> 'x' == 'X'
False

@jaccobtw all of the above, … and, those arguments (xoooxx) etc, must be strings.

@jaccobtw, a bit of extra material to peek at once you have this function going to your satisfaction:

def xo(s):
    if s.count('x') == s.count('o'):
        return True
    else:
        return False

… can be written a bit more concisely as

def xo(s):
    if s.count('x') == s.count('o'):
        return True      # The function halts when return is reached
    return False

… and even more so (maybe even Pythonic!):

def xo(s):
    return s.count('x') == s.count('o')       # s.count('x') == s.count('o') 
                                              # returns True or False all by itself, 
                                              # ... since it is a boolean comparison
1 Like

D’you know, I just chucked the test cases into a list so I could iterate over them to check the code… didn’t even think about the ''!

1 Like