"""
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
"""

def fib(k):
    ret = 0
    x, y = 1, 2
    while x <= k:
    #check if latest value of fib seq is even
        if x % 2 == 0:
            ret += x
        #continue with fib seq
        x, y = y, x + y
    return ret

#4000001 because  range function does not hit x
print fib(4000001)

