1

Code that I currently have, is as it appears not to be using the variable i initizied inside a if statement. I got the idea to do the code this way from a much older wiser programer. I am unsure why I am getting this error, is in some way my syntax off or is there something I am missing?

Below is segment of the code in question and the out put below that. As well after that, is the entire code for context. For the entire code, the code in question is down at the bottom in the processScores method. Thank you

    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

and now the output explaining the error

     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'

and now the total code, for context. Remember the code in question is more towards the bottom

    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 
Christopher Jakob
  • 91
  • 1
  • 2
  • 11

1 Answers1

2

You need to call the instance method on an instance (I assume that's what you get as the second argument of processScores(file, Score)), not the class you gave both of them the same name Score, so which is which?.

Change this line:

def processScores( file, Score):

To lowercase score:

def processScores( file, score):

and all following references to score, for example:

Score.initialScore(start)

To:

score.initialScore(start)

This will implicitly send the Score instance (score) as the first argument self, and the argument start as the second argument start.

See this answer to better understand the use of self.

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • i'll give that a try. if you don't mind @Reut Sharabani that kinda racks my brain and I don't understand how this all works with a lower case score. can you explain further? if not is there in anyway you could point me in the right direction? – Christopher Jakob Jan 17 '15 at 23:02
  • @ChristopherJakob when you use an instance method you always have `self` as the first argument, why is that? Well, that's because when you call `score.something(somearg)` you're **actually** calling `Score.something(score, somearg)`. I'll add a link to a detailed answer. – Reut Sharabani Jan 17 '15 at 23:04
  • ok cool thanks Reut Sharabani. This thing still isnt working for me but ill keep giving this a shot. looking forward to the documentation your sending. Again, Thank you – Christopher Jakob Jan 17 '15 at 23:05
  • @ChristopherJakob read the link in the bottom to better understand the use of `self` in python. The big picture is that Java, or other languages, implicitly use the instance itself when using instance methods. So if in java you have `public void instancemethod(x, y)`, in python you'd have `def instancemethod(self, x, y)`. This si more explicit, and tells you an instance of the class (`self`) is passed to the method. In your case the instance is `score` and the class is `Score`, you were confusing the two. – Reut Sharabani Jan 17 '15 at 23:10
  • I read the link and it has helped but im still not fully there I am going to keep on reading about self so I'm not going to bother you with other questions about self and waste your time. I do just want verification not to insult you in anyway, just as a way of ensuring that I am on the right path. def ProcessScores was made with the idea that it was out of the class. That it would then call upon the class to do the work. Is that something you took note of while answering my question? thank you very much – Christopher Jakob Jan 17 '15 at 23:28
  • You should come to chat and ask me and others so you can have a good grasp of what's going on: http://chat.stackoverflow.com/rooms/6/python – Reut Sharabani Jan 17 '15 at 23:38
  • @ChristopherJakob, also, if this answers your original question you should accept it. – Reut Sharabani Jan 17 '15 at 23:40