[Tutor] File I/O help

Chris Castillo ctcast at gmail.com
Thu Jul 23 05:48:49 CEST 2009


*I'm supposed to be reading in names a grades from a text file like so:
*
Chris
100
89
76
0
Dave
56
45
30
23
10
0
Sara
55
76
78
60
0

*the 0 indicates that's the end of the student's grades and not an actual 0
number grade for the student. This is what my code looks like so far:*

from myFunctions import *

grades = []
names = []
gradeTotal = 0
numStudents = 0

inputFile = open("input.txt", "r" )    # Read in a file with student names a
grades

for line in open("input.txt", "r"):    # iterate through txt file with names
and grades
    if line.strip().isdigit():
        grade = float(line)    # convert score into float type
        gradeTotal += grade    # adds score to running total
        if grade != 0:
            grade = grades.append(grade)
    else:
        name = line.strip()
        name = names.append(name)    # Append name to names list


studentTotal = str(len(names))    # Get student total
grades.sort() # Sort the grades in ascending order

---------------------------------------------------------------------------------------
*and then I need to output something that looks like this:*

Name of student      76,89,100 - and then their letter grade average(A, B,
C, etc..)
Name of student      76,89,100 - and then their letter grade average(A, B,
C, etc..)

*How do I do this with the code I have so far ( if possible )*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090722/731b4576/attachment.htm>


More information about the Tutor mailing list