Hello all! Thanks for viewing my question and thanks in advance for any help you may provide. I am writing a program that reads lines from a txt file and then prints the output in a certain fashion. Here they both are
Here is the txt file I am reading from
JOE FRITZ AMERICAN GOVERNMENT B
JOE FRITZ CALCULUS I A
JOE FRITZ COMPUTER PROGRAMMING B
JOE FRITZ ENGLISH COMPOSITION A
LANE SMITH FUND. OF DATA PROCESSING B
LANE SMITH INTERMEDIATE SWIMMING A
LANE SMITH INTRO. TO BUSINESS C
JOHN SPITZ CHOIR C
JOHN SPITZ COLLEGE STATISTICS B
JOHN SPITZ ENGLISH LITERATURE D
JOHN SPITZ INTRO. TO BUSINESS B
I am trying to get my output to look like this:
GRADE REPORT
NAME COURSE GRADE
-----------------------------------------------------------
JOE FRITZ AMERICAN GOVERNMENT B
CALCULUS I A
COMPUTER PROGRAMMING B
ENGLISH COMPOSITION A
Total courses taken = 4
LANE SMITH FUND. OF DATA PROCESSING B
INTERMEDIATE SWIMMING A
INTRO. TO BUSINESS C
Total courses taken = 3
JOHN SPITZ CHOIR C
COLLEGE STATISTICS B
ENGLISH LITERATURE D
INTRO. TO BUSINESS B
Total courses taken = 4
Total courses taken by all students = 11
Run complete. Press the Enter key to exit.
EDIT
Thanks to your help, I finished this program.
I know it may be ugly, but ATM I am just happy to have the output right.
Here is the source that will display the correct output:
#-----------------------------------------------------------------------
# VARIABLE DEFINITIONS
name = ""
previousName = ""
course = ""
grade = ""
grandTotal = 0
courseCount = 0
eof = False
#-----------------------------------------------------------------------
# CONSTANT DEFINITIONS
#-----------------------------------------------------------------------
# FUNCTION DEFINITIONS
def startUp():
global gradeFile, grandTotal,courseCount, previousName, name
grandTotal = 0
courseCount = 0
gradeFile = open("grades.txt","r")
print
print ("grade report\n").center(60).upper()
print "name".upper(),"course".rjust(21).upper(),"grade".rjust(33).upper()
print "-" * 60
readRecord()
def readRecord():
global name, course, grade, eof, courseCount
studentRecord = gradeFile.readline()
if studentRecord == "":
eof = True
else:
name = studentRecord[0:20]
course = studentRecord[20:50]
grade = studentRecord[50:51]
eof = False
def processRecords():
global courseCount, previousName, name, grandTotal
while not eof:
if name != previousName:
if name == "JOE FRITZ ":
courseCount = 0
print name + course + " " + grade
previousName = name
courseCount += 1
else:
print "\t\t Total courses taken =",courseCount
print
courseCount = 0
print name + course + " " + grade
previousName = name
courseCount += 1
else:
print (" " * 20) + course + " " + grade
courseCount += 1
grandTotal +=1
readRecord()
print "\t\t Total courses taken =",courseCount
def closeUp():
gradeFile.close()
print "\nTotal courses taken by all students =",grandTotal
#-----------------------------------------------------------------------
# PROGRAM'S MAIN LOGIC
startUp()
processRecords()
closeUp()
raw_input("\nRun complete. Press the Enter key to exit.")
Thanks for your help everyone. I really do appreciate it. Sorry if I have frustrated anyone during the process. Have a good one. Peace