繁体   English   中英

Python返回None而不是True / False

[英]Python Returns None instead of True/False

我正在制作一个简单的程序,该程序将在给定单词库,坐标和网格的情况下在网格区域中找到一个单词。 当我在底部使用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)

我的代码中有很多问题可以发现-

  1. 在您的函数grid_string_maker(word_block, x, y) ,您始终返回pos_word_vert ,而从不返回水平的。 这是因为每次调用该函数都是从头开始,而不是从上次中断的地方开始。 另外,您不需要那么多的ifs,您可以直接使用0,1,2作为索引,而不用在y == 1执行y-1 y == 1 ,因为您已经知道y是1, y-1是0,依此类推。您应该更改函数以将vert和水平字符串一起作为一个元组返回,然后将它们接受为单独的字符串。

  2. 在您的函数中find_word() -在您的第二个if..elif..else ,永远不会达到else ,因为if会检查两件事是否相等,而elif会检查是否相等,所以没有第三种选择。 取而代之的是,您应该从函数中返回False作为默认值, False是该函数直到那时都没有返回true。

  3. 另外,与其创建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"

第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内部的false,因此它直接退出而不返回任何内容。 最后的ELSE包包跳过(因为在ELSE IF之后它什么也不做),最终没有回报。 如果对第47行的if和第48行的非缩进进行散列,则向您显示问题(并返回True)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM