+ 15
Why it is showing error?[solved]
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] data.sort() sum=0 for i in range(1,(len(data)-1)): sum+=data[i] print(sum)
34 Respuestas
+ 7
#Try this
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78]
data.remove(max(data))
data.remove(min(data))
print(sum(list(data)))
+ 5
It's working just fine for me.
+ 3
I do not know the task text, and cannot understand why you sort the array and why the range is smaller. But you can try this:
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78]
sum=0
for i in range((len(data)):
sum+=data[i]
print(sum)
+ 2
No it's a code coach
+ 2
Atul [Inactive] Here are two ways to do this:
#1
data.remove(min(data))
data.remove(max(data))
print(sum(data))
Or just
#2
print(sum(sorted(data)[1:-1]))
# I hope that this helps. Happy coding!
+ 2
Delicate Cat In first one remove that braces inside data list,in print statement
And 2nd code is wrong
+ 2
Your first code worked Delicate Cat
+ 2
Atul [Inactive] then your code presented in the question should work. The idea with sort and smaller range in for loop is very good.
+ 1
Atul [Inactive] What's the question?
+ 1
You’re analyzing a data set and need to remove the outliers (the smallest and largest values.
The data is stored in a list).
Complete the code to remove the smallest and largest elements from the list and output the sum of the remaining numbers.
Remember the functions a list supports: min and max can be used to get the smallest and largest numbers.
+ 1
It doesn't pass the test cases. You can run it 😅
+ 1
Delicate Cat Didn't worked
+ 1
Yes you are correct Delicate Cat . Might be the edge cases
+ 1
Atul [Inactive] Would you mind posting the expected output? I guess that the test case is hidden. Should the code take any inputs?
+ 1
Atul [Inactive] maybe you missed sthg in the question. Delicate Cat has done 3 ways and they have to have worked ..
+ 1
Delicate Cat I observed that their is no error in logic but presicion
+ 1
Atul [Inactive] Does Simba 's code work? If it does, mine should work too; is that list() typecast really necessary?
+ 1
Atul [Inactive] There was a small typo in my code. Now, it works.
+ 1
Pakhi Sahu It's Python, not Javascript.