Home List object has AttributeError for a method in a self-defined class
Reply: 2

List object has AttributeError for a method in a self-defined class

quantumheels
1#
quantumheels Published in 2017-12-06 04:35:40Z

I'm trying to calculate weighted averages. We just started OOP and I'm not sure if my problem is with that or lists.

Here is my code:

class WeightedAverage:
    """ Class to compute weighted averages. """

    def __init__(self):
        """ Creates empty lists. """
        scoresList = []
        weightsList = []

    def addScore(self,score,weight):
        scoreBool = checkScore(score)
        weightBool = checkWeight(weight)

        if scoreBool == True and weightBool == True:
            scoresList.append(score)
            weightsList.append(weight)


    def checkScore(scr):
        if scr in range(1,10):
            scrBl = True
        else:
            scrBl = False
        return scrBl


    def checkWeight(wgt):
        if wgt in [0.5,1,1.5,2]:
            wgtBl = True
        else:
            wgtBl = False
        return wgtBl


    def getScores(self,scoresList):
        return scoresList

    def getWeights(self,weightsList):
        return weightsList

    def getWAvg(self,scoresList,weightsList):
        wAvg = [scoresList*weightsList for scoresList,weightsList in zip(scoresList,weightsList)]
        return wAvg


# For testing
def main():
    scoresData = [1.5,3,9.2]
    weightsData = [0.5,2,1]

    wAvg = scoresData.getWAvg(scoresData,weightsData)
    print(wAvg)    # should display 15.95/3

Where main() is defined as a means of testing the code in my class.

When I run main(), I get the following TraceBack:

Traceback (most recent call last):
  File "H:\WeightAverages.py", line 58, in <module>
    main()
  File "H:\WeightAverages.py", line 54, in main
    wAvg = scoresData.getWAvg(scoresData,weightsData)
AttributeError: 'list' object has no attribute 'getWAvg'

Is this a problem with my class or is it just a list issue? This is my first time writing OO code so I'm not entirely sure this is even right!

Manoj Jadhav
2#
Manoj Jadhav Reply to 2017-12-06 04:39:54Z

you should do this

class WeightedAverage(object):
""" Class to compute weighted averages. """

    def __init__(self):
        """ Creates empty lists. """
        scoresList = []
        weightsList = []

    def addScore(self,score,weight):
        scoreBool = checkScore(score)
        weightBool = checkWeight(weight)

        if scoreBool == True and weightBool == True:
            scoresList.append(score)
            weightsList.append(weight)


    def checkScore(scr):
        if scr in range(1,10):
            scrBl = True
        else:
            scrBl = False
        return scrBl


    def checkWeight(wgt):
        if wgt in [0.5,1,1.5,2]:
            wgtBl = True
        else:
            wgtBl = False
        return wgtBl


    def getScores(self,scoresList):
        return scoresList

    def getWeights(self,weightsList):
        return weightsList

   def getWAvg(self,scoresList,weightsList):
        wAvg = [scoresList*weightsList for scoresList,weightsList in zip(scoresList,weightsList)]
        return wAvg


# For testing
def main():
    scoresData = [1.5,3,9.2]
    weightsData = [0.5,2,1]
    obj = WeightedAverage()
    wAvg = obj.getWAvg(scoresData,weightsData)
    print(wAvg)
sergio verduzco
3#
sergio verduzco Reply to 2017-12-06 04:49:59Z

Manoj Jadhav's comment seems to be right. In order to call the getWAvg function, you need to create an instance of the WeightedAverage class.

Here's a version of your code where an instance is created, and used to call the getWavg function. Also notice I changed getWAvg.

class WeightedAverage:
    """ Class to compute weighted averages. """

    def __init__(self):
        """ Creates empty lists. """
        scoresList = []
        weightsList = []

    def addScore(self,score,weight):
        scoreBool = checkScore(score)
        weightBool = checkWeight(weight)

        if scoreBool == True and weightBool == True:
            scoresList.append(score)
            weightsList.append(weight)


    def checkScore(scr):
        if scr in range(1,10):
            scrBl = True
        else:
            scrBl = False
        return scrBl


    def checkWeight(wgt):
        if wgt in [0.5,1,1.5,2]:
            wgtBl = True
        else:
            wgtBl = False
        return wgtBl


    def getScores(self,scoresList):
        return scoresList

    def getWeights(self,weightsList):
        return weightsList

    def getWAvg(self,scoresList,weightsList):
        wAvg = sum([s*w for s,w in zip(scoresList,weightsList)]) / len(scoresList)
        return wAvg


# For testing
def main():
    scoresData = [1.5,3,9.2]
    weightsData = [0.5,2,1]

    instance = WeightedAverage()
    wAvg = instance.getWAvg(scoresData,weightsData)

    print(wAvg)    # should display 15.95/3
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.343167 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO