I am trying to add all of the negative integers in a user input list, but the function always returns 0 as the answer. It works if I include the list as part of the function, but not when its user input. The code I have is:
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]]
thefourtheye
225k52 gold badges442 silver badges485 bronze badges
asked Nov 2, 2015 at 2:50
1
sum[i for i in alist if i < 0]
Done. It's Python, it has to be simple!
answered Nov 2, 2015 at 3:09
Dima TisnekDima Tisnek
10.7k4 gold badges62 silver badges116 bronze badges
remove the second line of your code, it sets the input to an empty list no matter what your input is.
answered Nov 2, 2015 at 3:41
You assign an empty list to userList every time you enter the function, how can it not be 0?
Further more, the function input[] seems not able to handle a list of inputs.
So I think you can do this:
def sumNegativeInts[userList]:
sum = 0
for i in userList:
if i < 0:
sum += i
return sum
inputData = raw_input['some prompts']
userList = [int[i] for i in inputData.split[' ']]
print[sumNegativeInts[userList]]
answered Nov 2, 2015 at 3:03
sunhssunhs
3911 silver badge6 bronze badges
You could always just do if and elif statements to determine whether or not to add a number to a list and then take the sum of the list. My prompt is making a list of 100 number with random integers from -100 to 100. Then finding the sum of all the negative numbers.
import random
def SumNegative[]:
my_list = []
for i in range[100]:
x = random.randint[-100, 100]
if x > 0:
pass
elif x < 0:
my_list.append[x]
print[sum[my_list]]
SumNegative[]
answered May 15, 2019 at 22:20
1
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a list. The task is to find the sum of Negative, Positive Even, and Positive Odd numbers present in the List.
Examples:
Input: -7 5 60 -34 1 Output: Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6 Input: 1 -1 50 -2 0 -3 Output: Sum of negative numbers is -6 Sum of even positive numbers is 50 Sum of odd positive numbers is 1
Negative numbers are the numbers less than 0 while positive even numbers are numbers greater than 0 and also divisible by 2. 0 is assumed to be a positive even number, in this case.
Approach:
- The first approach input a list of numbers from the user.
- Two loops are run, one for the positive numbers and the other for the negative numbers, computing the numbers’ summation.
- If n is the size of the list of numbers,
- it takes O[2n] time complexity, for iterating over the list of numbers twice.
Python3
class
Sumofnumbers:
def
negSum[
self
,
list
]:
neg_sum
=
0
for
num
in
list
:
num
=
int
[num]
if
[num <
0
]:
neg_sum
+
=
num
print
[
"Sum of negative numbers is "
, neg_sum]
def
posSum[
self
,
list
]:
pos_even_sum
=
0
pos_odd_sum
=
0
for
num
in
list
:
num
=
int
[num]
if
[num >
=
0
]:
if
[num
%
2
=
=
0
]:
pos_even_sum
+
=
num
else
:
pos_odd_sum
+
=
num
print
[
"Sum of even positive numbers is "
,
pos_even_sum]
print
[
"Sum of odd positive numbers is "
,
pos_odd_sum]
list_num
=
[
-
7
,
5
,
60
,
-
34
,
1
]
obj
=
Sumofnumbers[]
obj.negSum[list_num]
obj.posSum[list_num]
Output:
Sum of negative numbers is -41 Sum of even positive numbers is 60 Sum of odd positive numbers is 6
The second approach computes the sum of respective numbers in a single loop. It maintains three counters for each of the three conditions, checks the condition and accordingly adds the value of the number in the current sum . If n is the size of the list of numbers, it takes O[n] time complexity, for iterating over the list of numbers once.
Python3
class
Sumofnumbers:
def
Sum
[
self
,
list
]:
neg_sum
=
0
pos_even_sum
=
0
pos_odd_sum
=
0
for
num
in
list
:
num
=
int
[num]
if
[num <
0
]:
neg_sum
+
=
num
else
:
if
[num
%
2
=
=
0
]:
pos_even_sum
+
=
num
else
:
pos_odd_sum
+
=
num
print
[
"Sum of negative numbers is "
,
neg_sum]
print
[
"Sum of even positive numbers is "
,
pos_even_sum]
print
[
"Sum of odd positive numbers is "
,
pos_odd_sum]
list_num
=
[
1
,
-
1
,
50
,
-
2
,
0
,
-
3
]
obj
=
Sumofnumbers[]
obj.
Sum
[list_num]
Output:
Sum of negative numbers is -6 Sum of even positive numbers is 50 Sum of odd positive numbers is 1
Method: Using list comprehension
Python3
lst
=
[
1
,
-
1
,
50
,
-
2
,
0
,
-
3
];i
=
0
x
=
[i
for
i
in
lst
if
i>
0
and
i
%
2
=
=
0
]
y
=
[i
for
i
in
lst
if
i>
0
and
i
%
2
!
=
0
]
z
=
[i
for
i
in
lst
if
i
0
and
i
%
2
=
=
0
],lst]]
y
=
list
[
filter
[
lambda
i: [i>
0
and
i
%
2
!
=
0
],lst]]
z
=
list
[
filter
[
lambda
i: [i
0
]
y
=
[
int
[i]
for
a,i
in
enumerate
[lst]
if
int
[i]
%
2
!
=
0
and
int
[i]>
0
]
z
=
[
int
[i]
for
a,i
in
enumerate
[lst]
if
int
[i] operator, known as greater than or lesser than the operator.