Why is my private variable causing me so much trouble in python. Guys pls suggest me changes in my code to make it error free. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is my private variable causing me so much trouble in python. Guys pls suggest me changes in my code to make it error free.

My CODE: class Difference: def __init__(self, a): self.__elements = a def computeDifference(self): self.__elements = sorted(self.__elements) def maximumDifference(self): return (self.__elements[-1]-self.__elements[0])

10th Dec 2019, 6:25 PM
Aditya Singh
Aditya Singh - avatar
3 Answers
+ 2
Next time, it would be better, if you put your code in playground and post the link here. That's easier to test for all.
10th Dec 2019, 9:39 PM
Coding Cat
Coding Cat - avatar
+ 1
Try this: class Difference: def __init__(self, a): self.__elements = a def computeDifference(self): return max(self.__elements) - min(self.__elements) test = [5, 2, 8, 1, 6] dif = Difference(test) print(test) print('differenz',dif.computeDifference())
10th Dec 2019, 9:19 PM
Coding Cat
Coding Cat - avatar
0
Please specify what are your problems in relation to your variable, so we can help you.
10th Dec 2019, 7:39 PM
Gonçalo Magalhaes
Gonçalo Magalhaes - avatar