列表中负整数的总和

-2 python sum list negative-number python-3.x

我试图添加用户输入列表中的所有负整数,但该函数始终返回 0 作为答案。如果我将列表作为函数的一部分包含,但在用户输入时则不起作用。我的代码是:

def sumNegativeInts(userList):
    userList = []
    sum = 0
        for i in userList:
            if i < 0:
            sum = sum + i
            return sum

userList = input("Enter a list of +/- integers: ")
print(sumNegativeInts(userList))
Run Code Online (Sandbox Code Playgroud)

Dim*_*nek 5

sum(i for i in alist if i < 0)
Run Code Online (Sandbox Code Playgroud)

完毕。它是Python,它必须简单!