2

I think my problem is very simple but I cannot figure it out. I am trying to add the even indices of the list into the variable . The error persists on the last line of the function . 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()
1
  • 1
    I've just run this code with few modifications, the iteration works. Can you be more specific as to what error is thrown? Commented Apr 25, 2020 at 15:47

2 Answers 2

1

There are no errors in your code but it does nothing because:

  1. You don't return your result, even, from oddEvenSame function
  2. In your main function you don't use the returned values from oddEvenSame invocation.

Here are the minor changes you should do:

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

    # Call <oddEvenSame()>
    print(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]
    return even

main()

As a side note, you may use slicing instead of loop in oddEvenSame func:

def oddEvenSame(n):
    digits = [int(i) for i in str(n)]
    return sum(digits[::2])
Sign up to request clarification or add additional context in comments.

Comments

0

Haha, what a silly mistake! Thank you... I just learned functions this week. Here is the final program:

Assignment 12 "Odd-Even" - due 5.1.2020

Ask the user to enter a number. The number is valid if the odd position digits add up to the even position digits. For example 1234 is not valid because 1+3 != 2+4 but 1232 is valid because 1+3 = 2+2. Your program should use functions as we have discussed in the videos. In particular: Your program should take in the input from the user in a main() function. The main function should call a function called oddEvenSame with the inputted number. The oddEvenSame function should return True if the number it gets is valid otherwise it should return False. You should output Invalid or Valid in your main function depending on what oddEvenSame returned. Note: You may assume the user will enter a number with an even number of digits

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

        # Call <oddEvenSame()>
        if oddEvenSame(n) == True:
            print("Valid") 
        else:
            print("Invalid")  

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

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

        # Add odd indecies of <digits> into <odd>    
        odd = 0
        for k in range(1, len(digits), 2):
            odd += digits[k]

        # Check if odd digits add up to even digits 
        if odd == even:
            return True
        else:
            return False

    # Call <main()>
    main()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.