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

code is ignoring variable created in if statement or is it something else? Python

我目前拥有的代码似乎没有使用我在 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 

您需要在 实例 上调用实例方法(我假设这就是您作为 processScores(file, Score) 的第二个参数得到的),而不是 class 你给了他们两个相同的名字Score,所以哪个是哪个?

更改此行:

def processScores( file, Score):

小写score:

def processScores( file, score):

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

Score.initialScore(start)

收件人:

score.initialScore(start)

这将隐式发送 Score 实例 (score) 作为第一个参数 self,并将参数 start 作为第二个参数 start .

请参阅 this answer 以更好地了解 self 的用法。