[Tutor] File I/O help
steve at alchemy.com
Thu Jul 23 09:38:59 CEST 2009
On Wed, Jul 22, 2009 at 10:48:49PM -0500, Chris Castillo wrote:
> *I'm supposed to be reading in names a grades from a text file like so:
My Spidey Senses are picking up "homework assignment", so I'll try to
nudge you in a general direction without doing your work for you.
> from myFunctions import *
I'm curious about this... is this a way for you to organize your code
into multiple modules? I'd suggest looking at dividing things up into
classes or modules by topic, not just a "dumping ground for a bunch of
functions I use often" (which is what I'm guessing here).
> inputFile = open("input.txt", "r" ) # Read in a file with student names a
> for line in open("input.txt", "r"): # iterate through txt file with names
> and grades
You're opening the file twice, which probably isn't what you wanted.
> 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)
> name = line.strip()
> name = names.append(name) # Append name to names list
> studentTotal = str(len(names)) # Get student total
Why do you want to turn the number of students into a string? While
you're at it, look at the return value from the append method for
lists. What are you accomplishing by assigning that back onto the
variables grade and name above?
> grades.sort() # Sort the grades in ascending order
All the grades? Seems like you need a way to keep track of the
grades for each individual student? Maybe some kind of collection
object which can store things by name?
Steve Willoughby | Using billion-dollar satellites
steve at alchemy.com | to hunt for Tupperware.
More information about the Tutor