因为函数只能返回一次。在The return statement terminates the execution of a function and
returns control to the calling function. Execution resumes in the
calling function at the point immediately following the call.
所以在第8行,您返回sum,然后就存在了。它不会进行循环的下一次迭代,因此它返回第一个num,这对于该条件是真的。在Solution:
将每个迭代结果存储在list、dict等位置,然后在函数的最后返回结果。在
你可以试试:#creat list using function
def Negative(List):
neg_num=[]
for i in List:
if i<0:
neg_num.append(i)
return neg_num
#input and print list
inputData= input('enter numbers ')
List = [float(i) for i in inputData.split(' ')]
print(Negative(List))
输出:
^{pr2}$
正如你所说:how to count the numbers obtained from the list??
试试这个:#creat list using function
def Negative(List):
neg_n