On 18/01/14 22:52, Jackie Canales wrote:

def processScores(filename):
     infile = open(filename, 'r')
     line = infile.readlines()
     infile.close()
     lineNum = 0
     s = Score(0)

     for item in line2:
>          lineNum += 1

What is line2? I assume you mean line?

You could do all of the above more easily with

with open(filename) as infile:
    for lineNum, line in enumerate(infile):

But line will *always* be a string so you need to convert the string into data that Python can understand. int() works if the line contains a numerical string.
But it won't work for lists, and neither will list() for that matter.
You need to remove the brackets then split the lstring by commas and then convert each element. Its probably complex enough to warrant a function of its own...


         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:

You probably want to test type before trying to convert to an int(). Unfortunately the type will be a string so the list test will always fail. You need to actually examine the string content. (Or you can trust the file to tell you, the instructions say a list is flagged by an m/M in the line before. So instead of throwing that data away you can use it to direct your program what kind of data to process.


                 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

else: pass

does nothing so you may as well remove it.

     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:

o
10
m
[3, 12, 1]

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').


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

Correct

so am I suppose to write a loop that enables an exception error.

I'm not sure what you mean by that, but you do need to write a
function to interpret a string representation of a list.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to