lundi 2 janvier 2017

Python variable acting weird

I am making a pattern recogniser and i run into a VERY strange error, if the pattern is recognised it prints out ["x", "x", "2"] and i set variable worked to t. But when i print out worked on the last line it prints out ["x", "x", "x"]

(See the part of code marked # _____________ IMPORTANT __________ #)

    import random

    def check_pattern(pattern, rule):
        for i, RULE in enumerate(pattern):
            if RULE != "x":
                if rule[i] != RULE:
                    return False
        return True

    if __name__ == "__main__":
        numbers = [002,212,212,432,132,142,322,992,972,692,942,472]

        #numbers = [a.replace("\n", "") for a in numbers]

        print("Finding pattern in {}".format(numbers))

        patterns = []
        possible = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "x"]

        found = False
        pattern = ["x", "x", "x"]
        working = []
        worked = []
        for i, rule in enumerate(pattern):
            for attemptRule in possible:
                testRule = pattern
                testRule[i] = str(attemptRule)
                works = True

                for num in numbers:
                    numList = [a for a in num]
                    if check_pattern(testRule, numList):
                        pass
                        #print("Pattern {} works with {}".format(t, numList))
                    else:
                        works = False
                        #print("Pattern {} doesnt work with {}".format(t, numList))
                if works:
                    # ____________________ IMPORTANT ______________________ #
                    if testRule != ['x', 'x', 'x']:
                        print(testRule)                    
                        worked = testRule
                    # ____________________ IMPORTANT ______________________ #
                    working.append(testRule)
        #print("Pattern {} works".format(working))
    print(worked)

Thanks.

Aucun commentaire:

Enregistrer un commentaire