Jon Morgan
Jon Morgan

Reputation: 3

Python For loop range by step with step increments

I wasn't sure how to even word this, and it's so basic I feel like an idiot. I'm new to any programming and am trying to figure out how to create a text file that has a header task that shows the range of the loop step and then followed by multiple tasks that have the sequential range of the header per task. How do I fit the range of the loop so that each sub task doesn't incrementally go up. Thanks for all help.

Here's my really bad code:

#!/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()

And below is what it returns:

  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

Upvotes: 0

Views: 2665

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799082

You forgot to reset addTask2 each iteration.

Upvotes: 1

Related Questions