# Author: James Tam # Version: November 29 2010 # V3 # Shows how you can check for the newline character in String. # V2 # This version will convert the letter grade to a numerical # GPA which will then be written to another (output) file. # V1 # A program by James Tam that reads in letter grades from a file called # 'letters.txt' and displays the grades onscreen. # Get information about the file name at run time from the user. inputFileName = raw_input ("Enter the name of input file to read the grades from: ") outputFileName = raw_input ("Enter the name of the output file to record the GPA's to: ") # Open file for reading, confirm file with user. inputFile = open (inputFileName, "r") outputFile = open (outputFileName, "w") # Update user on what is happening. print "Opening file", inputFileName, " for reading." print "Opening file", outputFileName, " for writing." # While we haven't read past the end of the file continue reading from # it. gpa = 0 for line in inputFile: if (line[1] == '\n'): print "newline" if (line[0] == "A"): gpa = 4 elif (line[0] == "B"): gpa = 3 elif (line[0] == "C"): gpa = 2 elif (line[0] == "D"): gpa = 1 elif (line[0] == "F"): gpa = 0 else: gpa = -1 # Add onto the end of the string the newline marker # gpa on it's own line in the output file). temp = str (gpa) temp = temp + '\n' print line[0], '\t', gpa # Write the gpa (converted from a number to a string). Because the gpa is # followed by a newline each gpa will reside on it's own line in the output # file. outputFile.write (temp) # Finished writing to file, provide feedback to user and close file. inputFile.close () outputFile.close () print "Completed reading of file", inputFileName, print "Completed writing to file", outputFileName,