Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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三重嵌套for循环未完全迭代_Python - Fatal编程技术网

Python三重嵌套for循环未完全迭代

Python三重嵌套for循环未完全迭代,python,Python,基本上,当我运行这段代码时,我的程序的“print block[j]”部分会运行,但之后我得到的所有测试代码,即print block[k]和print“testing code”都没有到达。我已经盯着这个程序看了半个小时了,只是不明白为什么这个程序可以超越第二个嵌套循环,而不能超越第三个嵌套循环 def SwapII(str): new_str = "" for letter in str: if letter.isupper(): new_str = new_s

基本上,当我运行这段代码时,我的程序的“print block[j]”部分会运行,但之后我得到的所有测试代码,即print block[k]和print“testing code”都没有到达。我已经盯着这个程序看了半个小时了,只是不明白为什么这个程序可以超越第二个嵌套循环,而不能超越第三个嵌套循环

def SwapII(str): 
  new_str = ""
  for letter in str:
    if letter.isupper():
      new_str = new_str + letter.lower()
    else:
      new_str = new_str + letter.upper()

  new_list = new_str.split(' ')


  for i in new_list:
    block = i.split()

    for j in range(len(block)):
        print block[j]
        for k in range(j+1, len(block)):
            print block[k]
            if block[j].isdigit() and block[k].isdigit():
                lala = block[j]
                block[j] = block[k]
                block[k] = lala
                print "testing code"



    i = ''.join(block)


  return ' '.join(new_list)



# keep this function call here  
# to see how to enter arguments in Python scroll down
print SwapII(raw_input()) 

你有没有仔细检查过
len(block)
不等于
1
i=''。join(block)
是没有意义的。您没有在任何地方使用此值。您是否尝试过使用调试器单步执行代码?添加一个导入pdb;pdb.set_trace()在内部for循环之前。有关调试的更多信息可以在此处找到:在j循环的
之前尝试
打印块。您可能会对这个方法感兴趣:这个代码到底应该做什么,您的算法打算如何达到这个目标?将字符串按空格分割,然后迭代单词。然后,您再次按空格拆分这些单词,并尝试迭代这些单词,但它们当然不能再拆分了。您希望这段代码做什么?