Thank you! I think I have working in the right direction. I have one more question related to this module.
I had to move everything to a single module, but what I would like to do is have this class in a file by itself so I can call this from other modules. when it was in separate modules it ran with all 0's in the output. Here is the code in one module: #import Statistics class Statistics: def __init__(self, *value_list): self.value = value_list self.square_list= [] def mean(self, *value_list): try : ave = sum(self.value) / len(self.value) except ZeroDivisionError: ave = 0 return ave def median(self, *value_list): if len(self.value) <= 2: n = self.mean(self.value) elif len(self.value) % 2 == 1: m = (len(self.value) - 1)/2 n = self.value[m+1] else: m = len(self.value) / 2 m = int(m) n = (self.value[m-1] + self.value[m]) / 2 return n def variance(self, *value_list): average = self.mean(*self.value) for n in range(len(self.value)): square = (self.value[n] - average)**2 self.square_list.append(square) try: var = sum(self.square_list) / len(self.square_list) except ZeroDivisionError: var = 0 return var def stdev(self, *value_list): var = self.variance(*self.value) sdev = var**(1/2) return sdev def zscore(self, x, *value_list): average = self.mean(self.value) sdev = self.stdev(self.value) try: z = (x - average) / sdev except ZeroDivisionError: z = 0 return z a = [1,2,3,4,5,6,7,8,9,10] stats = Statistics(*a) mean = stats.mean(*a) median = stats.median(*a) var = stats.variance(*a) stdev = stats.stdev(*a) z = stats.zscore(5, *a) print(mean, median, var, stdev, z) print() On Wed, Feb 24, 2010 at 7:33 PM, Alan Gauld <alan.ga...@btinternet.com>wrote: > > "James Reynolds" <eire1...@gmail.com> wrote > > I understand, but if self.value is any number other then 0, then the "for" >> will append to the square list, in which case square_list will always have >> some len greater than 0 when "value" is greater than 0? >> > > And if value does equal zero? > > Actually I'm confused by value because you treat it as both an > integer and a collection in different places? > > > Is this an occasion which is best suited for a try:, except statement? Or >> should it, in general, but checked with "if's". Which is more expensive? >> > > try/except is the Python way :-) > > > def variance(self, *value_list): >> if self.value == 0: >> var = 0 >> else: >> average = self.mean(*self.value) >> for n in range(len(self.value)): >> square = (self.value[n] - average)**2 >> self.square_list.append(square) >> var = sum(self.square_list) / len(self.square_list) >> return var >> > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor