Sum of negative numbers python
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: Show
thefourtheye 225k52 gold badges442 silver badges485 bronze badges asked Nov 2, 2015 at 2:50
1
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:
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.
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:
Python3
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
Output: Sum of negative numbers is -6 Sum of even positive numbers is 50 Sum of odd positive numbers is 1 Method: Using list comprehensionPython3
Output even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6 Method: Using the lambda functionPython3
Output even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6 Method: Using enumerate functionPython3
Output even positive numbers sum 50 odd positive numbers sum 1 negative numbers sum -6 How do you add negative numbers to a list in Python?Python program to print negative numbers in a list. Input: [2, 6, -10, -3, 1, -9]. Output: [-10, -3, -9]. Input: [-2, -4, 5, -3, 0, 6, -10]. Output: [-2, -4, -3, -10]. Does Python work with negative numbers?How do you know if Python is positive or negative? Python tests whether a number is greater than zero or lesser than zero. It is done through the < and > operator, known as greater than or lesser than the operator.
|