I am making a simple program that will find a word in grid area given a word bank, coordinates and the grid. When I use the print statement at the bottom, instead of receiving True or False, python returns "None". My code can be seen below and any improvements are appreciated. Thank you:
def grid_string_maker(word_block, x, y): # creates string from grid
pos_word_vert = ""
pos_word_hor = ""
if y == 0:
pos_word_vert += word_block[y][x]
pos_word_vert += word_block[y+1][x]
pos_word_vert += word_block[y+2][x]
return pos_word_vert
elif y == 1:
pos_word_vert += word_block[y-1][x]
pos_word_vert += word_block[y][x]
pos_word_vert += word_block[y+1][x]
return pos_word_vert
elif y == 2:
pos_word_vert += word_block[y-2][x]
pos_word_vert += word_block[y-1][x]
pos_word_vert += word_block[y][x]
return pos_word_vert
if x == 0:
pos_word_hor += word_block[y][x]
pos_word_hor += word_block[y][x+1]
pos_word_hor += word_block[y][x+2]
return pos_word_hor
elif x == 1:
pos_word_hor += word_block[y][x-1]
pos_word_hor += word_block[y][x]
pos_word_hor += word_block[y][x+1]
return pos_word_hor
elif x == 2:
pos_word_hor += word_block[y][x-2]
pos_word_hor += word_block[y][x-1]
pos_word_hor += word_block[y][x]
return pos_word_hor
def find_word(word_block, x, y, validate_words):
pos_word_vert = grid_string_maker(word_block, x, y)
pos_word_hor = grid_string_maker(word_block, x, y)
if pos_word_vert == validate_words: #Checks if the string vertically from up to down matches
return True
elif pos_word_vert != validate_words: #If top to bottom doesnt match, it reverses
rev_pos_word_vert = pos_word_vert[::-1]
if rev_pos_word_vert == validate_words: #Checks if the reversed bottom to top matches
return True
if pos_word_hor == validate_words: #Checks if the string vertically from up to down matches
return True
elif pos_word_hor != validate_words: #If top to bottom doesnt match, it reverses
rev_pos_word_hor = pos_word_hor[::-1]
if rev_pos_word_hor == validate_words: #Checks if the reversed bottom to top matches
return True
else:
return False
validate_words = set("THE")
word_block = [
["A","A","A"],
["T","H","E"],
["A","A","A"]
]
print find_word(word_block, 2, 1, validate_words)
There are lots of issues in your code that I can spot -
In your function grid_string_maker(word_block, x, y)
, you always return pos_word_vert
, you never return the horizontal one. This is because each time you call the function it starts from the starting, not where you left off. Also, you do not need so many ifs , you can directly use 0,1,2
as indices instead of doing y-1
if y == 1
, since you already know if y
is 1 , y-1
is 0, etc. You should change the function to return both vert and horizontal strings together as a tuple and then accept them into separate strings.
In your function - find_word()
- In your second if..elif..else
, the else
would never be reached, since your if checks whether two things are equal, and your elif checks if they are unequal, there is no third alternative. Instead of that, you should return False
as default from the function, if it did not return true till then.
Also, instead of creating validate_words
as set, create it as string. There is no benefit of using set
for this variable.
Code -
def grid_string_maker(word_block, x, y): # creates string from grid
pos_word_vert = word_block[0][x] + word_block[1][x] + word_block[2][x]
pos_word_hor = word_block[y][0] + word_block[y][1] + word_block[y][2]
return (pos_word_vert, pos_word_hor)
def find_word(word_block, x, y, validate_words):
pos_word_vert, pos_word_hor = grid_string_maker(word_block, x, y)
if pos_word_vert == validate_words: #Checks if the string vertically from up to down matches
return True
else: #If top to bottom doesnt match, it reverses
rev_pos_word_vert = pos_word_vert[::-1]
if rev_pos_word_vert == validate_words: #Checks if the reversed bottom to top matches
return True
if pos_word_hor == validate_words: #Checks if the string vertically from up to down matches
return True
else: #If top to bottom doesnt match, it reverses
rev_pos_word_hor = pos_word_hor[::-1]
if rev_pos_word_hor == validate_words: #Checks if the reversed bottom to top matches
return True
return False
validate_words = "THE"
Line 47 is your issue with the current word_block value:
elif pos_word_hor != validate_words: #If top to bottom doesnt match, it reverses
rev_pos_word_hor = pos_word_hor[::-1]
if rev_pos_word_hor == validate_words: #Checks if the reversed bottom to top matches
return True
the problem is that the else if returns true so it enters that statement but the if inside of it is false, so it simply exits without returning anything. The ELSE catchall at the end skips (because it does nothing following the ELSE IF) and there ends up being no return. If you hash out the if on line 47 and unindent line 48 that shows you the issue (and returns True).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.