1

I have got this function in my program.

def time(transcriptionFile) :
    ''' This function changes the time values in secs from the transcription file and keeps a list of start time and duration for each phoneme.'''
    with open("transcriptions.txt", "r") as tFile :
        timeList = []
        parameterList = []
        frame = 0.04
        lines = 0
        for line in tFile :
            lines += 1
            for i in range(lines) : 
                i = i * frame
            timeList.append(i)
            li = line.split()
            if li :
                start_time = (int(li[0]) / 10000000.)
                end_time = (int(li[1]) / 10000000.) 
                duration = ((int(li[1]) -int(li[0]))/10000000.)

                poly = poly_coeff(start_time, end_time, i)
                Newton(poly, parameterList) 

I want to use i as an argument in poly_coeff. The number of times this function is called is the number of lines in the file. And i is increasing with each line. So when the function is called for the first time the first i value should should be passed as argument then second time the second i value should be passed and so on.

I have certainly done it wrong here I can see but can't figure out how to do it in a right way. Also I guess too much is going on inside a single function. Is it better to split it? But everything is to do with opening the same file.

3 Answers 3

1

If you want the line number then you should do

poly = poly_coeff(start_time, end_time, lines)

If you want to use line number * frame then do

poly = poly_coeff(start_time, end_time, lines * frame)

I dont know what you are trying to do with

for i in range(lines) : 
    i = i * frame
Sign up to request clarification or add additional context in comments.

1 Comment

yes, I could have done this. I should have a list of lines * frame so I had used the for loop. Thanks
0

i is only in scope within the for loop. Add a declaration before it so you have access to it later.

<snip>
for line in tFile :
            lines += 1
            i = 0
            for i in range(lines) : 
                i = i * frame
<snip>

... and yes, I vote for more functional programming; break it out a bit to make it more readable.

2 Comments

This is actually incorrect, loops do not create a new scope and any names created in the loop are usable after.
@F.J I thought so too, until I tried a cut down version of his code and it failed with a variable uninitialized error.
0

edit: It looks like you might actually have an indentation error, did you intend to have everything starting with timelist.append(i) inside of the loop with i? That would make more sense.

        ...
        for i in range(lines): 
            i = i * frame
            timeList.append(i)
            li = line.split()
            if li :
                start_time = (int(li[0]) / 10000000.)
                end_time = (int(li[1]) / 10000000.) 
                duration = ((int(li[1]) -int(li[0]))/10000000.)

                poly = poly_coeff(start_time, end_time, i)
                Newton(poly, parameterList) 

If the indentation is correct, then I don't think that for loop with i is doing what you think it is, here is an example of what is going on:

>>> for i in range(3):
...   print "before", i
...   i = i * 0.04
...   print "after", i
... 
before 0
after 0.0
before 1
after 0.04
before 2
after 0.08
>>> print i
0.08

So basically, after the loop i is equivalent to (lines-1)*frame. You modify i during each iteration of the loop but it is set back to the next number in the range at the beginning of each iteration.

1 Comment

I need to make a list of increasing i values as well therefore I used it and it was giving me the proper values. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.