the names and course and grade dont print, just the totals
Click Here
it reads a file text file
Click Here
the output should look like
Click Here
Im barely learning how to use the control break method and most of the program is broken up in functions.
Im not looking for any fancy functions just the basic that dont deviate from what Ive learned in basic python.
any help is appreciated though :D

Please use the Code button in the forum's editor to post your code

# VARIABLE DEFINITIONs

currentName = ""
course = ""
grade = ""
categoryTotal = 0
eof = False
grandTotal=0
gradeFile = ""

#-----------------------------------------------------------------------
# CONSTANT DEFINITIONS

#-----------------------------------------------------------------------
# FUNCTION DEFINITIONS
def startUp():
    global gradeFile,currentName,previousName,course,grade,eof
    print ("grade report\n").center(60).upper()
    print "name".upper(),"course".rjust(22).upper(),"grade".rjust(32).upper()
    print "-" * 60
    gradeFile = open("grades.txt","r")
    readRecord()
    previousName=""
    previousName= currentName


def readRecord():
    global currentName, course,grade,eof,studentRecord,gradeFile
    studentRecord = gradeFile.readline()
    if studentRecord == "":
            eof = True
    else:
            currentName = studentRecord[0:20].strip()
            course = studentRecord[20:50].strip()
            grade = studentRecord[50:51].strip()
            eof = False

def gradesLoop():
    global eof,previousName,currentName,categoryTotal,course,grade
    while not eof:
        if  currentName != previousName:
                categoryChange()
                if categoryTotal <= 1:
                    tempStr = currentName.ljust(20)
                    tempStr+= course.ljust(50)
                    tempStr+= grade.ljust(51)
                else:
                    tempStr+= course.ljust(50)
                    tempStr+= grade.ljust(51)


        categoryTotal+=1
        readRecord()

def categoryChange():
    global categoryTotal,grandTotal,previousName,currentName
    print "Category count=", categoryTotal
    grandTotal+= categoryTotal
    categoryTotal=0
    previousName = currentName

def closeDown():
    global grandTotal, gradeFile
    categoryChange()
    print "Total courses taken by all students =", grandTotal
    gradeFile.close()
#------------------------------------------------------------------------
startUp()
gradesLoop()
closeDown()

raw_input("\nRun complete. Press the Enter key to exit.")

The data file

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

and the expected output

                       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.

I think the 'else' at line 47 is with the wrong if.

Edited 3 Years Ago by Gribouillis

looking the long lists of variable names declared global seems strange. If everything is global why you use functions? The function use looks otherwice nice, but reconsider the parameters of them.

Edited 3 Years Ago by pyTony

What and where do you and to print. I don't see anything in the program that prints name, etc. See Click Here for more info on reading files. Instead, we generally use something like

fp_in = open(file_name, "r")
for record in fp_in:
    # do some stuff
    print record

Edited 3 Years Ago by woooee

Hopefully this is not confusing. You can also group all of the records for each name into a list and then process the list containing all of the records for that name.

## Not Tested
## assumes the file is already in name order
## and just reads and prints  each group

def startUp():
    print ("grade report\n").center(60).upper()
    print "name".upper(),"course".rjust(22).upper(),"grade".rjust(32).upper()
    print "-" * 60
    previousName = ""
    list_of_grades=[]
    gradeFile = open("grades.txt","r")
    for rec in gradeFile:
        name, course, grade=readRecord(rec)  
        if name != previousName:
            previousName=name
            if len(list_of_names):
                gradesLoop(list_of_names)
            list_of_names = []  ## remove previous recs
        list_of_names.append([name, course, grade])
    if len(list_of_names):
        gradesLoop(list_of_names)

def readRecord(rec_in):
    currentName = rec_in[0:20].strip()
    course = rec_in[20:50].strip()
    grade = rec_in[50:51].strip()

    return currentName, course, grade

def gradesLoop(list_of names):
    grades_total = 0
    for name, course, grade in list_of_names:
        print name
        print "    ", course, grade
        grades_total += float(grade)
    print "Testing: total of all grades", grades_total

     print "-" *60
 #-----------------------------------------------------------------------
startup()

raw_input("\nRun complete. Press the Enter key to exit.")

Edited 3 Years Ago by woooee

We aren't using for loops neither lists. I just need it to print every time it changes category or name. So I put the statement to control if it was on the first categoryTotal loop it only print NAME, COURSE, GRADE and the else statement would print the rest COURSE,NAME

Edited 3 Years Ago by Fernando_1

i forgot to add that but once i did the temporary string didnt output correctly, which confused me even further :\

Try just a print statement in the if/else instead of creating tempStr. How you format the print depends on which version of Python you are using. For the "older", version 2.X way Click Here

            name_to_print = ""
            if categoryTotal <= 1:
                name_to_print = currentName

            print name_to_print, course, grade ## formatted correctly

Edited 3 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.