Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何对代码重新排序以获得正确的迭代计数器_Python_Iteration_Counter - Fatal编程技术网

Python 如何对代码重新排序以获得正确的迭代计数器

Python 如何对代码重新排序以获得正确的迭代计数器,python,iteration,counter,Python,Iteration,Counter,下面是一段代码,我需要返回正确的索引,根据“else”部分,在每次迭代中应该减少1: def findBestShift(wordList, text): splited_text = text.split(' ') index=26 while index >= 0: for item in splited_text: if item in wordList: return index #

下面是一段代码,我需要返回正确的索引,根据“else”部分,在每次迭代中应该减少1:

def findBestShift(wordList, text):

    splited_text = text.split(' ')
    index=26

    while index >= 0:
        for item in splited_text:
            if item in wordList:
                return index # in this place I need to get decreased index depends on the amount of iterations, instead of 26 which I always get
            else:
                index-=1
                return findBestShift(wordList,applyShift(item,index))
如何重新排序代码以返回减少的索引?除了这个小问题,我的代码工作正常

此代码的目的是通过此方法->解密字符串

我们有一些编码的单词,然后尝试通过移动字母表中的字母并将单词与单词列表进行比较来解码它们

若解码的单词与单词列表中的单词匹配,我们需要返回数字(shift),
如果单词彼此不匹配,则将字母表再移动(减少)1个字母,然后再次比较单词。

将索引初始化为“findBestShift”之外的26,并将其作为参数传递给findBestShift

您的函数是迭代和递归的。我不知道您想做什么,但通常它是迭代的或递归的,但不是两者都是…问题是,在索引递减之后,返回findBestShift(),它在执行时将index=26。这可以通过将index设置为findBestShift()的参数或在findBestShift()之前和之外初始化它来解决。