for循环问题

Tom*_*Kay 0 python for-loop

我们刚刚在课堂上学习了大约五分钟的循环,我们已经有了实验室.我正在努力,但仍然没有得到我需要得到的东西.我想要做的是获取一个整数列表,然后只取奇数整数并将它们加起来然后返回它们,如果整数列表为[3,2,4,7,2,4,1, 3,2]返回值为14

def f(ls):
    ct=0
    for x in (f(ls)):
        if x%2==1:
            ct+=x
    return(ct)


print(f[2,5,4,6,7,8,2])
Run Code Online (Sandbox Code Playgroud)

错误代码读取

Traceback (most recent call last):
  File "C:/Users/Ian/Documents/Python/Labs/lab8.py", line 10, in <module>
    print(f[2,5,4,6,7,8,2])
TypeError: 'function' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)

kar*_*ikr 5

只是一些小错误:

def f(ls):
    ct = 0
    for x in ls:
    #       ^     Do not call the method, but just parse through the list  
        if x % 2 == 1:
            ct += x
    return(ct)
    #     ^  ^ parenthesis are not necessary 

print(f([2,5,4,6,7,8,2]))
#      ^               ^    Missing paranthesis
Run Code Online (Sandbox Code Playgroud)