Python For循环逐步增加步长

时间:2013-05-18 22:57:27

标签: python

我不知道怎么说这个,而且它是如此基本我觉得自己像个白痴。我是任何编程的新手,我正在试图弄清楚如何创建一个文本文件,其中包含一个标题任务,显示循环步骤的范围,然后是多个任务,每个任务具有标题的顺序范围。如何适应循环的范围,以便每个子任务不会逐渐增加。感谢您的帮助。

这是我非常糟糕的代码:

#!/usr/bin/python

list = []
for i, x in enumerate(range(1,15,4)):
    test = range(x,max(0,min((x+4),16)))
    list.append(test)


foo = """
      Task -title{%(start)s:%(end)s}
      %(child)s
"""

foo2 = """
      Task -title{%(frame)s} -stuff
"""

addTask = ""
addTask2 = ""

for i in list:
    first = i[0]
    last = i[len(i)-1]
    for x in i:
        frm = foo2 % {"frame":x}
        addTask2+=(frm)
    new = foo % {"start":first,"end":last,"child":addTask2}
    addTask+=(new)


f = open("/tmp/test.txt","w")
f.write(addTask)
f.close()

以下是它的回报:

  Task -title{1:4}

  Task -title{1} -stuff

  Task -title{2} -stuff

  Task -title{3} -stuff

  Task -title{4} -stuff


  Task -title{5:8}

  Task -title{1} -stuff

  Task -title{2} -stuff

  Task -title{3} -stuff

  Task -title{4} -stuff

  Task -title{5} -stuff

  Task -title{6} -stuff

  Task -title{7} -stuff

  Task -title{8} -stuff


  Task -title{9:12}

  Task -title{1} -stuff

  Task -title{2} -stuff

  Task -title{3} -stuff

  Task -title{4} -stuff

  Task -title{5} -stuff

  Task -title{6} -stuff

  Task -title{7} -stuff

  Task -title{8} -stuff

  Task -title{9} -stuff

  Task -title{10} -stuff

  Task -title{11} -stuff

  Task -title{12} -stuff


  Task -title{13:15}

  Task -title{1} -stuff

  Task -title{2} -stuff

  Task -title{3} -stuff

  Task -title{4} -stuff

  Task -title{5} -stuff

  Task -title{6} -stuff

  Task -title{7} -stuff

  Task -title{8} -stuff

  Task -title{9} -stuff

  Task -title{10} -stuff

  Task -title{11} -stuff

  Task -title{12} -stuff

  Task -title{13} -stuff

  Task -title{14} -stuff

  Task -title{15} -stuff

1 个答案:

答案 0 :(得分:1)

您忘记在每次迭代时重置addTask2