Reputation: 23
Okay so I am given the following question: Given a list of integers, L, write code to determine whether the sum of the positive integers in the list is greater than the (absolute value of the) sum of the negative numbers in the list and print an appropriate message.
And this is what I came up with for code, but it doesn't work it simply returns the input numbers within brackets ex input=-1,-2,4,5 output=(-1,-2,4,5)
def question(L):
L = input()
sumPositive = () #List where I will send the positive integers from "L" list
sumNegative = () #List where I will send the negative integers from "L" list
if x in L >= 0:
append.x(sumPositive) #checks if the number is equal to or greater than 0, if so add it to "sumPositive" list
elif:
append.x(sumNegative) #if not add it to "sumNegative" list
if sum(sumPositive) > abs(sum(sumNegative)):
print "The sum of positive numbers is greater than the absolute value of negative numbers."
elif:
sum(Positive) < abs(sum(sumNegative)):
print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
else:
print "They are equal."
Can anyone tell me where I went wrong or if I'm going about something wrong. Thanks!
Upvotes: 0
Views: 2014
Reputation: 1282
def question(L):
sumPositive = [] #List where I will send the positive integers from "L" list
sumNegative = [] #List where I will send the negative integers from "L" list
for x in L:
if x>0:
sumPositive.append(x)
else :
sumNegative.append(x) #if not add it to "sumNegative" list
if sum(sumPositive) > abs(sum(sumNegative)):
print "The sum of positive numbers is greater than the absolute value of negative numbers."
elif sum(sumPositive) < abs(sum(sumNegative)):
print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
else:
print "They are equal."
question([1,-1,-2,-2])
You can apppend in a list. You have created a tuple. and you have to use a for loop to iterate through the input list. I could not understand why you used a if condition.
In python tuples are immutable
. You cannot change them. You can concatnate tuples. But I am sure that is not required in here. A list would suffice.
Upvotes: 0
Reputation: 9704
a much more pythonic solution :
def question(lst):
sum_pos = sum(x for x in lst if x >0)
sum_neg = sum(abs(x) for x in lst if x<0)
if sum_pos > sum_neg:
print "The sum of positive numbers is greater than the absolute value of negative numbers."
elif sum_pos < sum_neg:
print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
else:
print "They are equal."
question([1,-1,-2,-2])
Upvotes: 1
Reputation: 8709
>>> def question(x):
... s1=sum([i for i in x if i>0])
... s2=sum([abs(i) for i in x if i<0])
... if s1>s2:
... print "The sum of positive numbers is greater than the absolute value of negative numbers."
... elif s1<s2:
... print "The sum of absolute value of negative numbers is greater than the sum of positive numbers."
... else:
... print "They are equal."
...
>>> lst = [1,2,3,-1,-5,-7,3,-4,5,-6]
>>> question(lst)
The sum of absolute value of negative numbers is greater than the sum of positive numbers.
>>> lst=[-1,-2,3,2,3]
>>> question(lst)
The sum of positive numbers is greater than the absolute value of negative numbers.
>>> lst=[-2,-3,-3,3,2,3]
>>> question(lst)
They are equal.
Upvotes: 0
Reputation: 798676
()
creates an empty tuple, not a list. append()
is a list method, e.g. foo.append(bar)
.
Upvotes: 0