Basketball Players project in Python Data Science Course | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 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
Alvin Nguyen - avatar
8 Answers
+ 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
Alvin Nguyen - avatar
+ 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
Steven M - avatar
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
SRI SIVA LAKSHMANA REDDY DWARAMPUDI - avatar
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
Jan-Petter Fadum
Jan-Petter Fadum - avatar
0
This code is normal for pycharm, but not for sandbox in sololearn: import numpy as np players = np.array([180, 172, 178, 185, 190, 195, 192, 200, 210, 190]) s = [] mean = np.sum(players)/players.size variance = np.sum((players - mean)**2)/players.size std = np.sqrt(variance) for i in players: a = abs(i - mean) if 0 < abs(a-std) < 2: s.append(a) print(len(s))
29th Nov 2022, 7:26 PM
Timur Parshin
0
package main import "fmt" func main() { team := map[string]float32{ "P1": 1.98, "P2": 2.05, "P3": 1.89, "P4": 2.0, "P5": 2.11} var sum float32 = 0.0 for _, x := range team{ sum += x } fmt.Println(sum/5) }
17th Aug 2023, 8:27 PM
Prince Otieno
Prince Otieno - avatar