Python Error: Not all arguments converted during string formatting
<pre><code>def process_cars(text_file):
    total_cmpg = 0
    for line in text_file:
        if((line % 2) == 0):
            city_mpg = (line[52:54])
            print(city_mpg)
            city_mpg = int(city_mpg)
            total_cmpg += city_mpg
    print ("Total miles per gallon in the city:", total_cmpg)
</code></pre>

<p>The error comes in the if((line % 2) == 0): I have searched on the other questions with the same error but none of them could solve the problem. The error is: Not all arguments converted during string formatting. I want to mod the position of the line. For example if it is the third line then, 2 % 2.</p>

<pre><code>def process_cars(text_file):
    total_cmpg = 0
    for file_line_number, line in enumerate(text_file):
        if((file_line_number % 2) == 0):
            city_mpg = (line[52:54])
            print(city_mpg)
            city_mpg = int(city_mpg)
            total_cmpg += city_mpg
    print ("Total miles per gallon in the city:", total_cmpg)
</code></pre>

<p>Based on your comments, you want to only do the if statement every x amount of lines. Try what we have above, since we are using <code>enumerate()</code> which keeps a count of what ever is next in the object. In our case it keeps the count of the line number while still giving us what the line is. </p>

<p><code>file_line_number</code> is the file number line currently and <code>line</code> is the content of the line.</p>

python python-3.x