Jdbaba Jdbaba - 5 months ago 32
Python Question

Syntax error: bad input - Python

I have this code:

num = range(1,33)
num[0]=1
num[1]=2
for i in range(2,32):
num[i]=num[i-1]+num[i-2]


total=0
for i in range(0,32):
print num[i]
if num[i]%2==0:
total=total+num[i]
else:
num[i]=num[i+1]


I want to find the sum of the even numbers in the
num
array. Can anyone suggest what I did wrong here ?

Answer

Indentation is very important in python

if num[i]%2==0:
total=total+num[i]
else:
num[i]=num[i+1]

should be

if num[i]%2==0:
    total=total+num[i]
else:
    num[i]=num[i+1]

Also, use consistent indentation e.g 4 spaces every where you have to introduce indentation.