Hi there Stack Overflow!
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.
The task is to:
Find the last element (digit) on each line, if there are any, and sum all
that are even.
I have started to do something like this:
result = 0
counter = 0
handle = open('httpd-access.txt')
for line in handle:
line = line.strip()
digitFirst = int(line[counter].isdigit())
if digitFirst % 2 == 0:
result += digitFirst
ANSWER = result
18.104.22.168--[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
Don't even bother with the
isdigit. Go ahead and try the conversion to
int and catch the exception if it fails.
result = 0 with open('httpd-access.txt') as f: for line in f: try: i = int(line.strip()[-1:]) if(i % 2 == 0): result += i except ValueError: pass print('result = %d' % result)