0

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

这是我非常糟糕的代码:

#!/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
4

1 回答 1

1

您忘记重置addTask2每次迭代。

于 2013-05-18T23:01:47.053 回答