def test(): # do not change this or the next line!
numbers = [11.5, 28.3, 23.5, -4.8, 15.9, -63.1, 79.4, 80.0, 0, 67.4, -11.9, 32.6]
average = 0
# I need to write my code here so that it sets average
# to the average of the non-negative numbers
print(average)
return average # do not change this line!
# do not write any code below here
test() # do not change this line!
# do not remove this line!
Asked
Active
Viewed 1,369 times
-2

DYZ
- 55,249
- 10
- 64
- 93
-
What is your question? – DYZ May 27 '20 at 16:06
-
I need to write my code so that it sets average to the average of the non-negative numbers. – Elmer Gustavo Reyes Altamirano May 27 '20 at 17:31
-
SO is not a free coding service. You must demonstrate your effort. – DYZ May 27 '20 at 18:48
1 Answers
-1
Use numpy
.
STEPS:
- Find out the positive integers
- take a mean of those.
import numpy as np
def test(): # do not change this or the next line!
numbers = [11.5, 28.3, 23.5, -4.8, 15.9, -63.1, 79.4, 80.0, 0, 67.4, -11.9, 32.6]
num = np.array(numbers)
avg = np.mean((num[num >= 0]), axis=0)
return avg # do not change this line!
# do not write any code below here
test()
Output:
37.62222222222223
Edit:
def test(): # do not change this or the next line!
numbers = [11.5, 28.3, 23.5, -4.8, 15.9, -63.1, 79.4, 80.0, 0, 67.4, -11.9, 32.6]
num = [i for i in numbers if i>=0]
avg = sum(num)/len(num)
return avg # do not change this line!
# do not write any code below here
test()

Pygirl
- 12,969
- 5
- 30
- 43
-
Where should I add that to the existing code? It says "np" is not defined. Please see https://snipboard.io/XBVgS0.jpg – Elmer Gustavo Reyes Altamirano May 27 '20 at 17:37
-
do `import numpy as np`. The above is the complete code. You can add modify as per your requirement. – Pygirl May 27 '20 at 17:41
-
Sorry. This is for an online course and the platform does not allow modules like numpy. Would you mind providing a solution using the given code in my initial question? – Elmer Gustavo Reyes Altamirano May 27 '20 at 18:02
-
SO is not a free coding service. It is frown upon on SO to solve homework problems for someone who has not demonstrated any effort. – DYZ May 27 '20 at 18:47
-
I do agree. That's why I also provided the steps. I hope it's for learning purposes not for just doing the assignment. – Pygirl May 27 '20 at 20:23
-
As I mentioned above, this is not a homework but for an online course I taking for computational thinking. :) Thanks for the help guys. – Elmer Gustavo Reyes Altamirano May 28 '20 at 13:31