+ 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")

14th Feb 2022, 3:31 PM
Deepika
8 ответов
+ 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)
15th Feb 2022, 12:06 PM
Lothar
Lothar - avatar
+ 2
It is working fine nothing error here
14th Feb 2022, 3:58 PM
Abu Habban
Abu Habban - avatar
+ 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
14th Feb 2022, 5:22 PM
Simba
Simba - avatar
+ 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
14th Feb 2022, 5:59 PM
Simba
Simba - avatar
0
But it was coming try again
14th Feb 2022, 4:33 PM
Deepika
0
I used this but it was coming try again
14th Feb 2022, 5:25 PM
Deepika
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..
14th Feb 2022, 5:43 PM
Jayakrishna 🇮🇳
0
Ok
15th Feb 2022, 4:53 AM
Deepika