我正在制作一个简单的程序,它会在给定词库、坐标和网格的情况下在网格区域中找到单词。当我在底部使用 print 语句时,python 没有收到 True 或 False,而是返回“None”。我的代码可以在下面看到,任何改进都值得赞赏。谢谢:
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)
请您参考如下方法:
我可以发现您的代码中有很多问题 -
在你的函数
grid_string_maker(word_block, x, y)中,你总是返回pos_word_vert,你永远不会返回水平的。这是因为每次您调用该函数时,它都会从头开始,而不是您离开的地方。另外,你不需要那么多 ifs ,你可以直接使用0,1,2作为索引而不是做y-1ify == 1,因为您已经知道y是否为 1,y-1是否为 0,等等。您应该更改函数以将垂直和水平字符串一起返回为元组,然后将它们接受为单独的字符串。在您的函数中 -
find_word()- 在您的第二个if..elif..else中,else永远不会达到,因为你的 if 检查两个东西是否相等,而你的 elif 检查它们是否不相等,所以没有第三种选择。取而代之的是,如果函数到那时还没有返回 true,您应该返回False作为默认值。此外,不是按集创建
validate_words,而是将其创建为字符串。对此变量使用set没有任何好处。
代码-
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"
