[Tutor] Question in regards to my assignment

Jackie Canales jackiexxduh at yahoo.com
Sat Jan 18 23:52:11 CET 2014


I have with my assignment.


class Score:
    'A subclass of the object.'
    
    def __init__(self, init):
        'Sets the initial score to the object and sets the number of scores that have contributed to the total to 1.'
        self.x = 1
        self.init = int(init)
        self.total = int(init)
        
    
    def updateOne(self, amount=0):
        'Increases the total score of the object by amount and Increases the number of scores contributing to the total by 1.'
        self.x += 1
        self.total += amount
        return self.total
    
    def updateMany(self, lst=0):
        'Updates the total by the sum of all the scores in the list and number of scores contributing to the total by the number of items found in the list.'
        self.x += len(lst)
        self.total += sum(lst)
        return self.total

    
    def get(self):
        'Returns the current score in the object.'
        return self.total

    def average(self):
        'Returns the average of the scores that have contributed to the total score.'
        return self.total/self.x

    def __repr__(self):
        'Returns the canonical representation of the object.'
        return repr(self.total)
    
def processScores(filename):
    infile = open(filename, 'r')
    line = infile.readlines()
    infile.close()
    
   
    lineNum = 0
    s = Score(0)

    for item in line2:
        lineNum += 1
        
        if lineNum == 1:
            s.total = int(item)
            print("Score set to total value:", s.total)
        elif lineNum % 2:
            s.total = int(item)
            if type(item) == list:
                lstNum = s.updateMany(s.total)
                print("List detected. Updating many. Total outcome:", lstNum)
            else:
                intNum = s.updateOne(s.total)
                print("Numeric detected. Updating One. Total outcome:", intNum)
        else:
            pass
    print("The Final Score is:", s.get())
    print("The Average Score is:", s.average())
    print("The Score Object is:", s.__repr__())


The file that I am trying read is a text file call score:
5
o 
7
o 
10
m 
[3, 12, 1]
o
8
M 
[1, 2, 3]

The instruction for the assignment are as followed
The first line of the file is a number, which indicates the initial score for a Score object.  The remaining lines are pairs: the even-numbered lines contain a character and the odd-numbered lines contain either a number or a list.  The character will be one of 'o', 'O', 'm', or 'M', indicating that the next line contains either a single score ('o'  or 'O') or a list of scores ('m' or 'M').  The function will create a new Score object and then process each line or pairs of lines of the file by calling the appropriate method of the Score object.  As it processes each line or pairs of lines, it will print the information about the action being taken.  The first line will be a call to the constructor and the remaining pairs of lines will be calls either to the updateOne() or updateMany()methods.  Once the file has been processed, the function will print the final score, the average score, and return the Score object.


The error i am getting is:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    processScores('scores.txt')
  File "C:/Users/Michael/Documents/DePaul/tutor/hw2_practice.py", line 52, in processScores
    s.total = int(item)
ValueError: invalid literal for int() with base 10: '[3, 12, 1]\n'

So from my understanding when my code tries to read the first line with a list it is not able to read ''[3, 12, 1]\n'" because its not able to read "[" as a numeric value so am I suppose to write a loop that enables an exception error. I believe the this is what holding my final product from being complete.Believe is what my output is 

 processScores('scores.txt')
Score set to total value: 5
Numeric detected. Updating One. Total outcome: 14
Numeric detected. Updating One. Total outcome: 20
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    processScores('scores.txt')
  File "C:/Users/Michael/Documents/DePaul/tutor/hw2_practice.py", line 52, in processScores
    s.total = int(item)
ValueError: invalid literal for int() with base 10: '[3, 12, 1]\n'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140118/b0ea5155/attachment.html>


More information about the Tutor mailing list