1

This is the code for the main body. Which goes through a string of character formats them and then searches words in a inputed list. the string of characters is

'WAQHGTTWEECBMIVQQELSAZXWKWIIILLDWLFXPIPVPONDTMVAMNOEDSOYQGOBLGQCKGMMCTYCSLOACUZMXVDMGSXCYZUUIUNIXFNU'

and the string of words is 'UNIX CALPOLY GCC SLO COMPILE VIM TEST' my code correctly finds the words in all directions how ever the issue is when it doesn't find the word.

import funcs


def main():
    rawPuzzle = funcs.create_puzzle()
    words = funcs.get_words()
    funcs.print_puzzle(rawPuzzle)
    puzzle = funcs.format_puzzle(rawPuzzle)
    vertPuzzleRaw = funcs.make_vert_puzzle(puzzle)
    vertPuzzle = funcs.format_vert_puzzle(vertPuzzleRaw)
    for x in range(len(words)):
        word = words[x]
        for l in range(len(puzzle)):
            forward = funcs.check_forward(puzzle, word, l)
            if forward != -1:
                print(word, ':  (FORWARD)', 'row: ', l, 'column: ', forward)
            backward = funcs.check_backward(puzzle, word, l)
            if backward != -1:
                print(word, ':  (BACKWARD)', 'row: ', l, 'column: ', (9 - backward))
            down = funcs.check_down(vertPuzzle, word, l)
            if down != -1:
                print(word, ':  (DOWN)', 'row: ', down, 'column: ', l)
            up = funcs.check_up(vertPuzzle, word, l)
            if up != -1:
                print(word, ':  (UP)', 'row: ', (9 - up), 'column: ', l)
        if forward and backward and down and up == -1:
                print(word, ': word not found')


if __name__ == '__main__':
    main()

and the output looks like this:

Puzzle: 

WAQHGTTWEE
CBMIVQQELS
AZXWKWIIIL
LDWLFXPIPV
PONDTMVAMN
OEDSOYQGOB
LGQCKGMMCT
YCSLOACUZM
XVDMGSXCYZ
UUIUNIXFNU


UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX : word not found
UNIX :  (FORWARD) row:  9 column:  3
UNIX : word not found
CALPOLY :  (DOWN) row:  1 column:  0
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
CALPOLY : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
GCC : word not found
SLO : word not found
SLO : word not found
SLO : word not found
SLO : word not found
SLO : word not found
SLO : word not found
SLO : word not found
SLO :  (FORWARD) row:  7 column:  2
SLO : word not found
SLO : word not found
SLO : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE : word not found
COMPILE :  (UP) row:  6 column:  8
COMPILE : word not found
VIM : word not found
VIM :  (BACKWARD) row:  1 column:  4
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
VIM : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found
TEST : word not found

Process finished with exit code 0 I cant make it not print the "word not found" if it finds the word and I can't make it print it only once for when the words really is not found in the puzzle

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • 1
    Change `if forward and backward and down and up == -1:` to `if forward == backward == down == up == -1:` – Aran-Fey Feb 26 '18 at 23:55
  • @Aran-Fey when I do that the output changes to: `UNIX : (FORWARD) row: 9 column: 3 CALPOLY : (DOWN) row: 1 column: 0 CALPOLY : word not found GCC : word not found SLO : (FORWARD) row: 7 column: 2 SLO : word not found COMPILE : (UP) row: 6 column: 8 COMPILE : word not found VIM : (BACKWARD) row: 1 column: 4 VIM : word not found TEST : word not found` – Titouan Magret Feb 27 '18 at 00:10
  • Ah, the problem is that the values of `forward`, `backward`, `up` and `down` are overwritten every iteration, so when you check if they're all equal to -1, you're only checking if something was found in the last iteration. You're gonna need an extra variable: After `word = words[x]`, set `word_found = False`, in every `if ,,, != -1:` statement set `word_found = True`, and finally change `if forward and backward and down and up == -1:` to `if not word_found:`. – Aran-Fey Feb 27 '18 at 06:08

0 Answers0