[Tutor] Reading Data From File

Dave Angel davea at ieee.org
Sun Jul 26 12:57:19 CEST 2009


Chris Castillo wrote:
> ya i was kind of in a hurry composing the email earlier. sorry for the
> typos and misunderstanding. This is what I actually have.
>
> grades = []
> names = []
> gradeTotal = 0
> numStudents = 0
>
> inputFile = open("input.txt", "r")
>
> for line in inputFile:
>     if line.strip().isdigit():
>         grade = float(line)
>         if grade != 0.0:
>             gradeTotal += grade
>             grade = grades.append(grade)
>         else:
>             name = line.strip()
>             name = names.append(name)
>
>
> So even If I take the stuff out that you said, My structure still
> wouldn't be right because I'm processing everything all at once with
> no way to reference any of the grades with the student. How do I
> structure this differently to do that? That's what I'm having trouble
> with.
>
> On 7/25/09, Dave Angel <davea at dejaviewphoto.com> wrote:
>   
>> ctcast at gmail.com wrote:
>>     
>>> grades = []
>>> names = []
>>> gradeTotal = 0
>>> numStudents = 0
>>>
>>> inputFile = open("input.txt", "r"
>>>
>>> for line in inputFile:
>>> if line.strip().isdigit():
>>> grade = float(line)
>>> if grade != 0.0:
>>> gradeTotal += grade
>>> grade = grades.append(grade)
>>> else:
>>> name = line.strip()
>>> name = names.append(name)
>>>
>>> This just loops over the entire file basically and just continually
>>> adds the grades to the grades list and names to the names list. How do
>>> I just process each student's set of grades before moving on to the
>>> next one (since there is a zero terminating value telling the loop
>>> that a new student and his or her grades are about to follow)
>>>
>>> By the way I'm not worrying about determining the letter grade average
>>> right now, i'm importing a module I wrote after I figure this part out.
>>>
>>> On Jul 25, 2009 8:34am, bob gailer <bgailer at gmail.com> wrote:
>>>       
>>>> I concur with wesley and dave re homework.
>>>>         
>>> <snip>
>>>
>>>       
>> There are syntax errors of at least two kinds here.  The first is you're
>> missing a trailing parenthesis.  And the second is you lost all your
>> indentation when you retyped the code.  It'd really be better if you
>> pasted the actual code instead.  Not much of a problem in this case, at
>> least if I guess the same as you had, but in many cases the indentation
>> *is* the problem.
>>
>> Next problem is that you're assuming that list.append() returns
>> something useful. It doesn't return anything, which is to say it returns
>> "None."  So it's not useful to do:
>>      grade = grades.append(grade)
>>
>> just leave off the left half of that.  And likewise leave off the name=
>> from the other call to append().
>>
>> The next problem is that you have two independent lists, but no way to
>> correlate which elements of one correspond to which elements of the
>> other. So you have a choice to make.  Do you need all the data for
>> post-processing, or is it enough that you print it out, and discard it
>> afterwards?
>>
>> I'll assume that you'd answer that it's enough to just be able to print
>> it out.  In that case, you just need some well placed print statements.
>> Each time you come to a line with a zero in it, you have enough
>> information to print out one student's information.  And in this case,
>> you don't need a list of students, just the name of the current one.
>>
>> Do you expect any numbers to be non-integers?  I'd assume so because you
>> used the float() function instead of int().  But isdigit() is going to
>> be a problem if there's a decimal in there.
>>
>> DaveA
>>
>>
>>     
(In a mailing list like this one, putting a response at the top of your 
message is called top-posting, and makes it harder for the next person 
to see the sequence of messages.)

As I said before:
    *So you have a choice to make.  Do you need all the data for 
post-processing, or is it enough that you print it out, and discard it 
afterwards?*

I tried to assume an answer, but it looks like you stopped reading 
before that point.  So I'll try again, with a little more detail.

Each time you come to a line with a zero in it, you have enough 
information to print out one student's information.  You know the 
current student, you know all his scores.  So you could print it out at 
that point in the loop, rather than waiting till the entire program is past.

If you're not sure what I'm talking about, first put in a test for the 0 
line.  Add in a single print that prints out name and grades at that 
point.  You do know that you can print a list, just the same as any 
other variable?


Once you see you have enough data at that point, you'll have to make a 
minor change to eliminate this student's data from getting printed again 
for the next one.

Then it's just a question of formatting the print nicely.  So you 
replace the single print with a function call, passing it the name and 
grades, and format the information in that function.

And if you won't try what I said, then at least answer the question.

DaveA



More information about the Tutor mailing list