Python List Iteration/Addition

Hello, I think my problem is very simple but I cannot figure it out. I am trying to add the even indices of the list <digits> into the variable <even>. The error persists on the last line of the function <oddEvenSame>. I do not understand why you cannot iterate through the list with the for loop to add the indices? 

def main():
    # Get Input Number
    n = int(input("Enter a number:"))

    # Call <oddEvenSame()>
    oddEvenSame(n)


def oddEvenSame(n):
    # Split all digits of <n> into their own index within <digits>
    digits = [int(i) for i in str(n)]

    # Add even indices of <digits> into <even>
    even = 0
    for j in range(0, len(digits), 2):
        even += digits[j]

# Call <main()>
main()

What exactly do you expect the outcome to be? The code does not throw any exceptions and behaves like I’d expect it to: If I enter ‘1234’ as the string, “even” ends up as 4 (1 + 3).

One thing that surprises me though is that there’s no output of the result at all in your code.

1 Like