0

We had to calculate the players in the range of one standard deviations? My code is not excepted, however it works in other IDE

import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players)/len(players) var = sum((x-mean)**2 for x in players) / len(players) std_deviation = math.sqrt(var) print(std_deviation) upper_limit = int(mean + std_deviation) lower_limit = int(mean - std_deviation) z_v1 = players[0] - mean / std_deviation z_v2 = players[1] - mean / std_deviation z_v3 = players[2] - mean / std_deviation z_v4 = players[3] - mean / std_deviation z_v5 = players[6] - mean / std_deviation z_v6 = players[7] - mean / std_deviation z_v7 = players[8] - mean / std_deviation z_v8 = players[9] - mean / std_deviation z_v9 = players[10] - mean / std_deviation z_v_list = [z_v1, z_v2 , z_v3 , z_v4, z_v5, z_v6, z_v7, z_v8, z_v9] for x in z_v_list: if int(x) >= lower_limit and int(x) <= upper_limit: print(x) else: print("no value found in range of one standard deviation")

9th Oct 2021, 3:26 PM
Mugdha Bajpai
Mugdha Bajpai - avatar
8 odpowiedzi
+ 2
Only print the number if player in range – not anything else. And don't cast to integer, just leave it as float. Why do you modify the given players' values? I don't understand that part... (And why not use a loop?)
9th Oct 2021, 3:34 PM
Lisa
Lisa - avatar
+ 2
On line 18, you try to access element of list - players with index 10, which does not exist: z_v_list = players[10]... So your program gives error : index out of range.
9th Oct 2021, 3:35 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
It's a code project task, right? Read the instructions carefully: We need to print the number of players in range – not the player in range. I don't think you need z_v_list
9th Oct 2021, 3:59 PM
Lisa
Lisa - avatar
+ 2
What you're doing in the for loop makes no sense. The values in z_v_list are the ratios between the difference of player's heights from the mean and the standard deviation. If this ratio is between -1 and 1 the player's height is within a standard deviation of the mean. If you want to use lower_limit and upper_limit you can check if the player's height lies between them. At the end you have to print a single number, namely the number of players whose height is within one standard deviation of the mean. https://code.sololearn.com/c5IkLUJfn8w4/?ref=app
9th Oct 2021, 4:44 PM
Simon Sauter
Simon Sauter - avatar
0
import math players = [180, 172, 178, 185, 190, 195, 192, 200, 210, 190] mean = sum(players)/len(players) var = sum((x-mean)**2 for x in players) / len(players) std_deviation = math.sqrt(var) print(std_deviation) upper_limit = (mean + std_deviation) lower_limit = (mean - std_deviation) z_v1 = players[0] - mean / std_deviation z_v2 = players[1] - mean / std_deviation z_v3 = players[2] - mean / std_deviation z_v4 = players[3] - mean / std_deviation z_v5 = players[4] - mean / std_deviation z_v6 = players[5] - mean / std_deviation z_v7 = players[6] - mean / std_deviation z_v8 = players[7] - mean / std_deviation z_v9 = players[8] - mean / std_deviation z_v9 = players[9] - mean / std_deviation z_v10 = players[10] - mean / std_deviation z_v_list = [z_v1, z_v2 , z_v3 , z_v4, z_v5, z_v6, z_v7, z_v8, z_v9, z_v10] for x in z_v_list: if (x) >= lower_limit and (x) <= upper_limit: print(x) else: print("no value found in range of one standard deviation")
9th Oct 2021, 3:52 PM
Mugdha Bajpai
Mugdha Bajpai - avatar
0
The above code gives me the two values that is in the range
9th Oct 2021, 3:53 PM
Mugdha Bajpai
Mugdha Bajpai - avatar
0
But why here it doesn't work
9th Oct 2021, 3:53 PM
Mugdha Bajpai
Mugdha Bajpai - avatar
0
Thanks a lot .
9th Oct 2021, 7:35 PM
Mugdha Bajpai
Mugdha Bajpai - avatar