Error with .readlines()[n]
<p>I'm a beginner with Python.
I tried to solve the problem: "If we have a file containing &lt;1000 lines, how to print only the odd-numbered lines? ". That's my code:</p>

<pre><code>with open(r'C:\Users\Savina\Desktop\rosalind_ini5.txt')as f:
   n=1
   num_lines=sum(1 for line in f)
   while n&lt;num_lines:
         if n/2!=0:
                 a=f.readlines()[n]
                 print(a)
         break
    n=n+2
</code></pre>

<p>where <em>n</em> is a counter and <em>num_lines</em> calculates how many lines the file contains. 
But when  I try to execute the code, it says: </p>

<pre><code>"a=f.readlines()[n]
 IndexError: list index out of range"
</code></pre>

<p>Why it doesn't recognize <em>n</em> as a counter?</p>

<p>Well, I'd personally do it like this:</p>

<pre><code>def print_odd_lines(some_file):
    with open(some_file) as my_file:
        for index, each_line in enumerate(my_file):  # keep track of the index of each line
            if index % 2 == 1:  # check if index is odd
                print(each_line)  # if it does, print it

if __name__ == '__main__':
    print_odd_lines('C:\Users\Savina\Desktop\rosalind_ini5.txt')
</code></pre>

<p>Be aware that this will leave a blank line instead of the even number. I'm sure you figure how to get rid of it.</p>

python python-3.x while-loop