+ 2
I wrote coding but it asks to check the code. Is there any mistake?
Q) The given code includes a list of heights for various basketball players. You need to calculate and output how many players are in the range of one standard deviation from the mean.Output the result using print statement. Coding: import statistics import numpy as np players=[180,172,178,185,190,195,192,200,210,190] mean=statistics.mean([180,172,178,185,190,195,192,200,210,190]) stdev=statistics.stdev([180,172,178,185,190,195,192,200,210,190]) sum1=mean-stdev sum2=mean+stdev a=int(sum1) b=int(sum2) x=np.arange(a,b) print(x) result=[i for i in players if i in x] print(result) no_of_players=len(result) print(no_of_players,"players are in the range of one standard deviation from mean")
8 Respostas
+ 5
Deepika ,
please try the code. i only have modified the required issues, no optimization is done - but it should work. see my comments in the code:
# there should be no print output except the final result !!!
import statistics
import numpy as np
players=[180,172,178,185,190,195,192,200,210,190]
mean=statistics.mean([180,172,178,185,190,195,192,200,210,190])
stdev = int(statistics.pstdev([180,172,178,185,190,195,192,200,210,190])) # use additionally int(....), and should be pstdev(....)
sum1=mean-stdev
sum2=mean+stdev
a=int(sum1)
b=int(sum2)
x=np.arange(a,b)
result=[i for i in players if i in x]
no_of_players=len(result)
print(no_of_players)
+ 2
It is working fine nothing error here
+ 1
You only need to print no_of_players than other additional data.
Also, use np.std or variance method to calculate standard deviation since it's related to numpy
+ 1
Try using relational operators instead.
sum1 < i < sum2
Because, a should be greater than 178(178.12...) but, int() throws away the decimal portion.
So, it includes 178 in the list which shouldn't be there.
or
try using a= int(sum1)+1
0
But it was coming try again
0
I used this but it was coming try again
0
Try again means test case failing?
According to description, it asking to print only number of players in the range of standard deviation from mean.. So it needs print(no_of_players) #only , not need ' x, result ' to print
edit:
And also am not sure about, i found this. Is that same?
https://www.mathsisfun.com/data/standard-deviation-formulas.html
Hope it helps..
0
Ok