简体   繁体   English

Python 列表迭代/添加

[英]Python List Iteration/Addition

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. function的最后一行错误仍然存在。 I do not understand why you cannot iterate through the list with the for loop to add the indices?我不明白为什么你不能用 for 循环遍历列表来添加索引?

    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()

There are no errors in your code but it does nothing because:您的代码中没有错误,但它什么也不做,因为:

  1. You don't return your result, even , from oddEvenSame function您不会从oddEvenSame function 返回结果, even
  2. In your main function you don't use the returned values from oddEvenSame invocation.在您的main function 中,您不使用从oddEvenSame调用返回的值。

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:作为旁注,您可以在oddEvenSame func 中使用slicing而不是循环:

def oddEvenSame(n):
    digits = [int(i) for i in str(n)]
    return sum(digits[::2])

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作业 12“奇偶” - 2020 年 1 月 5 日到期

Ask the user to enter a number.要求用户输入一个数字。 The number is valid if the odd position digits add up to the even position digits.如果奇数 position 数字加起来偶数 position 数字,则该数字有效。 For example 1234 is not valid because 1+3.= 2+4 but 1232 is valid because 1+3 = 2+2.例如,1234 无效,因为 1+3.= 2+4,但 1232 有效,因为 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.您的程序应该在 main() function 中接收用户的输入。 The main function should call a function called oddEvenSame with the inputted number.主 function 应该调用一个 function 调用oddEvenSame 与输入的数字。 The oddEvenSame function should return True if the number it gets is valid otherwise it should return False.如果它得到的数字是有效的,oddEvenSame function 应该返回 True,否则它应该返回 False。 You should output Invalid or Valid in your main function depending on what oddEvenSame returned.您应该 output Invalid 或 Valid 在您的主 function 中,具体取决于 oddEvenSame 返回的内容。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM