私は加重平均を計算しようとしています。私たちはOOPを始めたばかりで、私の問題がそのリストにあるのかどうかは分かりません。リストオブジェクトに自己定義クラスのメソッドのAttributeErrorがあります
はここに私のコードである:()主は、私のクラスのコードをテストする手段として定義される
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
。
私はmain()の実行すると、私は以下のトレースバックを得る:
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'
が、これは私のクラスの問題ですか、それだけでリストの問題ですか?これはOOコードを書くのは初めてのことですので、これが正しいかどうかは完全にはわかりません!
あなたは '' 'WeightedAverage'''のインスタンスの作成を忘れましたか? –
これはPy2.7とPy3.6の両方でエラーなく実行されます。コードを更新しましたか? – Gary02127
いいえ、私は@ManojJadhavが提案したことを試してみました。 – quantumheels