6 Replies - 227 Views - Last Post: 29 January 2014 - 01:22 AM Rate Topic: -----

#1 pythonq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 14

range() in for loop

Posted 28 January 2014 - 10:54 PM

Hi,

I have a program in which i have to name my projects by numbering them. Each project has different tasks in them. These tasks also should be named by numbering them. For example: In my program i have a field projnum and tasknum in my input file. this should repeat for the number of times I give in my python code.

The code that i am using is as below.
for n in range(6):
    projnum = n
    for y in range(2):
        tasknum = (2 * n) + y




So as per the code i should have 6 projects and each projects should have 2 tasks. And the numbering should be as follows.
Project0 has Task0 and Task1
Project1 has Task2 and Task3
Project2 has Task4 and Task5
Project3 has Task6 and Task7
Project4 has Task8 and Task9
Project5 has Task10 and Task11

But my code is giving the below result
Project0 has Task1 and Task1
Project1 has Task3 and Task3
Project2 has Task5 and Task5
Project3 has Task7 and Task7
Project4 has Task9 and Task9
Project5 has Task11 and Task11

Could someone please help me fix this code?

Is This A Good Question/Topic? 0
  • +

Replies To: range() in for loop

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,100
  • Joined: 19-March 11

Re: range() in for loop

Posted 28 January 2014 - 11:00 PM

There's obviously some code missing here, but you're probably using tasknum for both numbers.
Was This Post Helpful? 0
  • +
  • -

#3 pythonq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 14

Re: range() in for loop

Posted 28 January 2014 - 11:05 PM

But when i remove the inner for loop, only the project numbers get named. I am not sure what could be missing as i am a beginner in python. Could you please help me corret this code? should i introdue any new functions?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,100
  • Joined: 19-March 11

Re: range() in for loop

Posted 28 January 2014 - 11:20 PM

It might be a little easier to help you if you post the rest of your code.
Was This Post Helpful? 0
  • +
  • -

#5 pythonq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 14

Re: range() in for loop

Posted 28 January 2014 - 11:39 PM

This is my python code:
header = """
	The List of Projects
"""

footer = """
    End of Project List
"""

template = open("projects.xml").read()

output = open("projects_output.xml","w")

output.write(header)

for n in range(6):
    projnum = n
    for y in range(2):
        tasknum = (2 * n) + y
	#write project
    #output.write(template % (vars()))

output.write(footer)

output.close()



This is my input file:
<Project name="PROJ_%(projnum)02d"
         projectID="Project%(projnum)02d">
        <Tasks>
                <Task taskID="Task%(tasknum)02d">
                </Task>
				<Task taskID="Task%(tasknum)02d">
                </Task>
        </Tasks>
</Project>



This is my output file:
The List of Projects
        <Project name="PROJ_00"
            projectID="Project00">
            <Tasks>
                <Task taskID="Task01">
                </Task>
				<Task taskID="Task01">
                </Task>
            </Tasks>
        </Project>
        <Project name="PROJ_01"
            projectID="Project01">
            <Tasks>
                <Task taskID="Task03">
                </Task>
				<Task taskID="Task03">
                </Task>
            </Tasks>
        </Project>        
		<Project name="PROJ_02"
            projectID="Project02">
            <Tasks>
                <Task taskID="Task05">
                </Task>
				<Task taskID="Task05">
                </Task>
            </Tasks>
        </Project>        
		<Project name="PROJ_03"
            projectID="Project03">
            <Tasks>
                <Task taskID="Task07">
                </Task>
				<Task taskID="Task07">
                </Task>
            </Tasks>
        </Project>        
		<Project name="PROJ_04"
            projectID="Project04">
            <Tasks>
                <Task taskID="Task09">
                </Task>
				<Task taskID="Task09">
                </Task>
            </Tasks>
        </Project>       
		<Project name="PROJ_05"
            projectID="Project05">
            <Tasks>
                <Task taskID="Task11">
                </Task>
				<Task taskID="Task11">
                </Task>
            </Tasks>
        </Project> 
 End of Project List



Correction in python code:
for n in range(6):
    projnum = n
    for y in range(2):
        tasknum = (2 * n) + y
    #write project
    output.write(template % (vars()))


This post has been edited by jon.kiparsky: 28 January 2014 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7292
  • View blog
  • Posts: 12,100
  • Joined: 19-March 11

Re: range() in for loop

Posted 29 January 2014 - 12:02 AM

That's interesting. So you define your template to depend on the code having certain particular names, and then you dump your whole vars dictionary to that template, and let python sort it out.

Not the way I'd do it, frankly. But you can see here why you're getting the same number for both tasks:

                <Task taskID="Task%(tasknum)02d">
                </Task>
				<Task taskID="Task%(tasknum)02d">
                </Task>


You're referring to one variable there, which can only have one value at a time. (the fact that it was recently set to another value has no bearing on the situation, of course)

So the the simplest solution would be to come up with another variable for the first task and set it to 2n. (you don't need that inner loop, of course - all it does is set one value, then overwrite it with a second value)

I'd actually suggest you rethink this whole approach, but that's your call.
Was This Post Helpful? 0
  • +
  • -

#7 pythonq  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 28-January 14

Re: range() in for loop

Posted 29 January 2014 - 01:22 AM

Thank you so much. I have named the variable differently.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1