Find the last element (digit) on each line and sum all that are even python 3
<p>Hi there Stack Overflow!</p>

<p>I'm trying to solve an assignment we got in my Python class today. I'm not super familiar with python yet so I could really need some tips.</p>

<p>The task is to:
Find the last element (digit) on each line, if there are any, and sum all
that are even.</p>

<p>I have started to do something like this:</p>

<pre><code>result = 0
counter = 0
handle = open('httpd-access.txt')
for line in handle:
    line = line.strip()
    #print (line)
    if line[:-1].isdigit():
        print(line[:-1].isdigit())
        digitFirst = int(line[counter].isdigit())
        if digitFirst % 2 == 0:
            print("second")
            result += digitFirst
    else:
        print("else")


    ANSWER = result
</code></pre>

<p>But this code doesnt work for me, I don't get any data in result.
What is it that i'm missing? Think one problem is that I'm not going through the line element by element, just the whole line.</p>

<p>Here is an example of how I line in the file can look:</p>

<pre><code>37.58.100.166--[02/Jul/2014:16:29:23 +0200]"GET/kod-exempel/source.php?dir=codeigniter/user_guide_src/source/_themes/eldocs/static/asset HTTP/1.1"200867
</code></pre>

<p>So the thing I want to retrieve is the 7. And then I want to do a check if the seven is a even or odd number. If it's even, I save it in the a variable.</p>

<p><code>isdigit()</code> return <code>True</code> or <code>False</code>, which is assigned to <code>digitFirst</code> (try print it!).<br>
<code>True</code> and <code>False</code> are evaluated as 0 and 1 (respectively) in math operations.<br>
So, it always pass the <code>if digitFirst % 2 == 0</code> when <code>digitFirst</code> is <code>0</code>, which means <code>0</code> always gets added to <code>result</code>.</p>

<p>Also, notice that <code>counter</code> is always 0 during the <code>for</code> loop and gets raised to 1 only after it, which means you are always working with the <strong>first</strong> letter of every line.  </p>

<p>The purpose of <code>counter</code>is unclear as it only used as "the index of the letter you get" each line.</p>

python python-3.x