我正在创建一个简单的程序,它会在网格区域中找到一个单词库,坐标和网格。当我在底部使用print语句时,python返回“None”而不是接收True或False。我的代码可以在下面看到,任何改进都值得赞赏。谢谢你:
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)
答案 0 :(得分:4)
我可以发现您的代码中存在许多问题 -
在您的函数grid_string_maker(word_block, x, y)
中,您始终返回pos_word_vert
,您永远不会返回水平的0,1,2
。这是因为每次调用该函数时,它都从启动开始,而不是从中断的位置开始。此外,您不需要这么多ifs,您可以直接使用y-1
作为索引而不是y == 1
y
,因为您已经知道y-1
是否为1,{ {1}}为0,等等。您应该更改函数以将顶点和水平字符串一起作为元组返回,然后将它们接受为单独的字符串。
在您的函数中 - find_word()
- 在您的第二个if..elif..else
中,永远不会达到else
,因为您检查两件事是否相等,以及您的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"
答案 1 :(得分:2)
第47行是当前word_block值的问题:
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 if返回true所以它进入该语句但if里面的if是false,所以它只是退出而不返回任何东西。最后的ELSE捕获跳过(因为它在ELSE IF之后什么都不做)并且最终没有返回。如果您在第47行和第8行显示问题并显示问题(并返回True)。