代码是忽略在if语句中创建的变量还是其他东西?蟒蛇

时间:2015-01-17 22:55:37

标签: python class variables

我目前拥有的代码,因为它似乎没有使用在if语句中启动的变量。我有这样的想法,从一个更老的更聪明的程序员这样做代码。我不确定为什么我收到此错误,在某种程度上我的语法关闭或者是否有我遗漏的东西?

以下是相关代码的一部分以及下面的输出。在那之后,是整个上下文代码。对于整个代码,有问题的代码位于processScores方法的底部。谢谢

    def processScores( file, Score):
#opens file using with method, reads each line with a for loop. If content  in line
#agrees with parameters in  if statements, executes code in if statment. Otherwise, ignores line    

    with open(file,'r') as f:
        for line in f:  #starts for loop for all if statements
            if line[0].isdigit: 
                start = int(line[0]) 
                Score.initialScore(start) #checks if first line is a number if it is adds it to intial score

现在解释错误的输出

     processScores('theText.txt',Score)
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    processScores('theText.txt',Score)
  File "C:/Users/christopher/Desktop/hw2.py", line 49, in processScores
    Score.initialScore(start) #checks if first line is a number if it is adds it to intial score
TypeError: initialScore() missing 1 required positional argument: 'start'

现在是总代码,用于上下文。请记住,有问题的代码更倾向于底层

    class Score:
# class to hold a running score, from object to parameter
# also to set number of scores that contribute to total of 1

    def __init__(self):
#initalizes the running score and score input accumilators
        self.runScore = 0
        self.scoreInputs = 0
        self.runScore = 0

    def initialScore(self, start):
#takes the initial score in line one of the file and updates
#the running score to the inital score
        self.runScore += start
        print('Grabing intial score from file, inital score set to ' + start)


    def updateOne (self, amount):
#updates running score by amount and Score input by 1
        self.runScore += amount
        self.scoreInputs += 1
        print('Adding ' + amount + ' to score, number of scores increased by 1. Current number of points scored ' + self.runScore + ',  current number of scores at ' + self.scoreInputs)

    def updateMany(self,lst):
#updates running score by the sum of the list and score inputs by the amount of
# number of items in the list
        self.runScore += sum(lst)
        self.scoreInputs += len(lst)
        print('Adding the sum of ' + len(lst) + 'scores to score. Score increased by ' +  sum(lst) + '. current number of points scored ' + self.runScore + ', current number of scores at ' + self.scoreInputs) 

    def get(self):
#returns the current score based on total amount scored
        print('Grabbing current score')
        print(self.runScore)

    def average(self):
#returns the average of the scores that have contributed to the total socre
        print('calculating average score')
        print(self.runScore // self.scoreInputs)

def processScores( file, Score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in  if statements, executes code in if statment. Otherwise, ignores line    

    with open(file,'r') as f:
        for line in f:  #starts for loop for all if statements
            if line[0].isdigit: 
                start = int(line[0]) 
                Score.initialScore(start) #checks if first line is a number if it is adds it to intial score

            if line == 'o' or line == 'O':
                amount = int(next(f))
                Score.updateOne(amount) #if line contains single score marker, Takes content in next line and
                                        #inserts it into updateOne
            if line == 'm'or line == 'M':
                scoreList = next(f)
                lst = []
                for item in scoreList: 
                    lst.append(item)
                    Score.updateMany(lst) # if line contains list score marker, creates scoreList variable and places the next line into  that variable
                                          #  creates lst variable and sets it to an empty list
                                          # goes through the next line with the            for loop and appends each item in the next line to the empty list
                                          # then inserts newly populated lst into updateMany

            if line == 'X':
                Score.get(self)
                Score.average(self) # if line contains terminator marker. prints total score and the average of the scores.
                                    # because the file was opened with the 'with' method. the file closes after 

1 个答案:

答案 0 :(得分:2)

你需要在实例上调用实例方法(我假设你得到的是processScores(file, Score)的第二个参数),而不是你给的两个类它们的名称相同Score,哪个是哪个?。

更改此行:

def processScores( file, Score):

小写score

def processScores( file, score):

以及以下对score的所有引用,例如:

Score.initialScore(start)

要:

score.initialScore(start)

这将隐式发送Score实例(score)作为第一个参数self,将参数start作为第二个参数start发送。 / p>

请参阅this answer以更好地了解self的使用情况。