- 2

# Basketball Players project in Python Data Science Course

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. Here is my code: ------------------------------------ import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] sum = 0 for nums in players: sum+=nums mean = sum / len(players) new_list = [] for i in players: new_list.append(mean-i) new_sum = 0 for k in new_list: new_sum+=k**2 variance = new_sum/len(players) deviation = math.sqrt(variance) #print(variance) #print(deviation) #print(round(variance)) #print(round(deviation)) num_players = [] for j in players: if j in range(round(deviation),round(mean)): num_players.append(j) else: pass print(len(num_players)) ------------------------------------------------- So what I did here is that I found the mean, variance, deviation of the list, I have checked and verified that the mean of this is 112 and the deviation is 11 by rounding them up. I created a new list that contains any numbers from the original list that are in range of 11,112 and I print out the len of that list. I did this in my code playground and I got 4. But the answer is wrong. I am not sure if anything is wrong with my code. Please help me. Thank you

10th Apr 2021, 11:27 PM
Alvin Nguyen
8 odpowiedzi
+ 1
OMG I AM SORRY I FIGURED THIS OUT AGAIN import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] sum = 0 for nums in players: sum+=nums mean = sum / len(players) new_list = [] for i in players: new_list.append(mean-i) new_sum = 0 for k in new_list: new_sum+=k**2 variance = new_sum/len(players) deviation = math.sqrt(variance) #print(variance) #print(deviation) #print(round(variance)) #print(round(deviation)) range1 = mean - deviation range2 = mean + deviation num_players = [] for j in players: if j in range(round(range1),round(range2)): num_players.append(j) else: pass print(len(num_players))
10th Apr 2021, 11:30 PM
Alvin Nguyen
+ 2
players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] # we need to find the SD # How many are in b/w mean and SD to be calculated sumofplyht = 0 for i in players: sumofplyht = sumofplyht + i median = sumofplyht / len(players) # Median of height variance = 0 for i in players: variance = variance + ((i - median)**2) sd = ((variance/len(players))**(1/2)) #Standard deviation count = 0 for i in players: if sd <= i <= mean: count = count+1 print("Players within the range of SD and Mean :",count) Answer i got is 4 but program says its wrong pls give the solution for this problem
18th May 2022, 7:04 PM
Deepak
+ 1
import numpy as np players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] players = sorted(players) data = np.array(players) mean = np.mean(data) std_dev = np.sqrt(np.var(data)) lower_bound = mean - std_dev upper_bound = mean + std_dev print(len([i for i in players if lower_bound <= i <= upper_bound]))
27th Feb 2023, 7:38 AM
Roman
0
That's really cool, awesome you got it figured out. For what it's with i tried to do the same thing, but with Pandas https://code.sololearn.com/cX325Sqr4QTl/?ref=app
11th Apr 2021, 3:20 AM
Steven M
0
Above answers are good .lets make it more smaller Use this to calculate mean and standard deviation :) import numpy mean=numpy.mean(players ) std=numpy.std(players)#standard deviation
5th Aug 2021, 5:53 AM
SRI SIVA LAKSHMANA REDDY DWARAMPUDI
0
I see there is a lot I can do to shorten my code, but I am a newbie, så I am proud to get it working at all. :-) import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players)/len(players). #calculate mean value std_dev = 0 #define variable for standard deviation for i in players: #calculate variance using a for-loop variance = (sum((i-mean)**2 for i in players)/len(players)) std_dev = (math.sqrt(variance)) #calculate standard deviation in_range = 0 for i in players: #count the number of players within the given range if i >= mean + (std_dev/2) or i <= mean - (std_dev/2): in_range += 1 else: continue print(in_range) #output number of players
8th Apr 2022, 10:43 AM