Basketball Players project in Python Data Science Course | Sololearn: Learn to code for FREE!

-1

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

4/10/2021 11:27:44 PM

Alvin Nguyen

4 Answers

New Answer

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

+2

players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players) / len(players) std = (sum([(i - mean) ** 2 for i in players]) / len(players)) ** 0.5 print(len(list(filter(lambda x: mean - std < x < mean + std, players))))

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

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