Search code examples
python-3.xwhile-looptic-tac-toe

Having problem in Tic-Tac-Toe game pattern?


when i run this code it return d['a'] for only j=1 what should i do to increment the value of j?

def pattern():
    d = {'a': '   |   |   ', 'b': '--- --- ---'}
    j = 1
    while j <= 11:
        if j not in [4,8]:
            return d['a']
        else:
            return d['b']
        j+=1

Solution

  • I see that you are trying to get the pattern one by one every time look executes. One alternative would be to put all the results in a array and then return the array.

    def pattern():
        d = {'a': '   |   |   ', 'b': '--- --- ---'}
        j = 1
        result_pattern = []
        while j <= 11:
            if j not in [4,8]:
                result_pattern.append(d['a'])
            else:
                result_pattern.append(d['b'])
            j+=1
        
        # return your array and loop over it after function call.
        return result_pattern 
    

    You will use your function something like this:

    p = pattern()
    for item in p:
        # do something with your result.