There are other answers already addressing specific bugs in your code, so I want to offer a completely different implementation that achieves your stated goal:
giving out correct total of first even fibonacci numbers upto 4 mn
If you want to find the sum of the even Fibonacci numbers up to some limit, the code below might be a more functional way of achieving it. It's based on composing Python generators, which should help make the code easier to follow and more reusable.
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def evens(l):
for x in l:
if x % 2 == 0:
yield x
def sum_even_fibonacci(limit):
total = 0
for x in evens(fib()):
if total + x > limit:
return total
total += x
if __name__ == '__main__':
print(sum_even_fibonacci(4000000))
Output
1089154
Edit
It's ambiguous what exactly OP is asking.
If OP wants to sum the even Fibonacci terms until the sum would surpass 4,000,000, then the answer is what I stated above - 1089154.
If OP wants to sum all even Fibonacci terms under 4,000,000, then the expression if total + x > limit would change to x > limit and the answer would be 4613732.
x=y; y= x+yis wrong.else: passis not necessary. Just remove it.4613732, not508186.