[Tutor] Error in loop. Please help.

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 28 01:31:05 CET 2004

Its late so I haven't looked too closely but I did spot a couple 
of things...

> f2r = f2.read()
> f2lines = f2r.split('\n')

Why not just use readlines() instead?

> f2lines = f2lines[1:-1]
> # -- (The above line filters the first element which
> is sample_name)

I think it will also filter the last line which you may not want!
Try using [1:] instead...

> ['a','b','c','d','e']
> out = open('test.sql','w')
> for m in range(len(samples)):

why not just use

for m in samples;

>     out.write("INSERT INTO sample(sample_name)VALUES("
> "'"+samples[m]+"'" ");" "\n")

     + m + ...

>     for d in range(len(f3lines)):

and similarly

for d in f3lines:

> a = samples.index(samples[m])

And won't this always return m?!
Hmm maybe not if there are more than one instance of 
whatever is at m, but I'm not sure that finding the first 
instance is what you think you are doing...

I don;t know if any of the above help solve the problem but 
they may make the code easier to read which is a start...

Alan G

More information about the Tutor mailing list