'''process a file of students''' 

def main(): 
    f = open( "Ex9_2010_input.txt", "r") 
    line = f.readline() 
    while len(line) != 0: 
        print line 
        line = f.readline() 

main()

In this case, the file contains comma separated values, each line representing student names and assessment marks in this format:
FirstName, LastName, Asg1Mark, Asg2Mark, ExamMark
Here are a couple of example lines from the file:
Fred, Nurke, 16, 17, 22
Jack, Sprat, 12, 13, 19


I need to modify the code i have written above to extract the different fields from each line of input and then calculate the final grade. and then i need Print out each student’s names and final grade.


Any insight in how i can approch this would be great

Thanks

Hint:

line='Fred, Nurke, 16, 17, 22'
line=line.split(',')
numbers=[float(mark) for mark in line[2:]]
average = sum(numbers)/len(numbers)
print "%s, %s: %.1f" % (line[0],line[1], average)

try using re.split("/W+")

'''process a file of students''' def calcGrade(asg1, asg2, exam): final = asg1 + asg2 + exam if final < 45: grade = "Fail" elif exam < 20: grade = "AE" elif final < 50: grade = "AA" elif final < 65: grade = "PS" elif final < 75: grade = "CR" elif final < 85: grade = "DI" else: grade = "HD" return grade

def main(): f = open( "A2Q1_input.txt", "r") line = f.readline() while len(line) != 0: #print line fields = line.split(",") #print fields fname = fields[0].strip() lname = fields[1].strip() asg1 = int (fields[2].strip()) asg2 = int (fields[3].strip()) exam = int (fields[4].strip())

print fname, lname, asg1, asg2, exam grade = calcGrade(asg1, asg2, exam) print "%s %s, Grade = %s" % (fname, lname, grade) line = f.readline() f.close()

main() </code>

this is what i have so far

with Fred, Nurke, 16, 17, 22 Jack, Sprat, 12, 13, 19

being in a serperate input file[code=php]
'''process a file of students'''
def calcGrade(asg1, asg2, exam):
final = asg1 + asg2 + exam
if final < 45:
grade = "Fail"
elif exam < 20:
grade = "AE"
elif final < 50:
grade = "AA"
elif final < 65:
grade = "PS"
elif final < 75:
grade = "CR"
elif final < 85:
grade = "DI"
else:
grade = "HD"
return grade


def main():
f = open( "A2Q1_input.txt", "r")
line = f.readline()
while len(line) != 0:
#print line
fields = line.split(",")
#print fields
fname = fields[0].strip()
lname = fields[1].strip()
asg1 = int (fields[2].strip())
asg2 = int (fields[3].strip())
exam = int (fields[4].strip())

print fname, lname, asg1, asg2, exam
grade = calcGrade(asg1, asg2, exam)
print "%s %s, Grade = %s" % (fname, lname, grade)
line = f.readline()
f.close()

main()
</code>

this is what i have so far

with
Fred, Nurke, 16, 17, 22
Jack, Sprat, 12, 13, 19

being in a serperate input file

Your code seems to work well, except your grade assignments are a little strange to me.

Here would be a slightly different approach to some of your code:

""" assume these are the contents of file student.txt
Fred, Nurke, 16, 17, 22
Jack, Sprat, 12, 13, 19
"""

# create a list of each student's
# [firstName, lastName, asg1Mark, asg2Mark, examMark] data list
studentList = []
for line in open("student.txt"):
    tempList = []
    for item in line.split(","):
        # strip leading spaces and trailing newline char
        item = item.strip()
        #print item  # test
        tempList.append(item)
        #print temp_list  # test
    studentList.append(tempList)

print studentList  # test

for student in studentList:
    firstName, lastName, asg1Mark, asg2Mark, examMark = student
    totalMarks = int(asg1Mark) + int(asg2Mark) + int(examMark)
    # test
    print "%s %s total mark = %d" % (firstName, lastName, totalMarks)

Your did little mistake with tags, you can use the button (code) to add code tags for selection.

'''process a file of students''' 
def calcGrade(asg1, asg2, exam):
    final = asg1 + asg2 + exam
    if final < 45:
        grade = "Fail"
    elif exam < 20:
        grade = "AE"
    elif final < 50:
        grade = "AA"
    elif final < 65:
        grade = "PS"
    elif final < 75:
        grade = "CR"
    elif final < 85:
        grade = "DI"
    else:
        grade = "HD"
    return grade


def main():
    f = open( "A2Q1_input.txt", "r")
    line = f.readline() 
    while len(line) != 0:
        #print line
        fields = line.split(",")
        #print fields
        fname = fields[0].strip()
        lname = fields[1].strip()
        asg1 = int (fields[2].strip())
        asg2 = int (fields[3].strip())
        exam = int (fields[4].strip())

        print fname, lname, asg1, asg2, exam
        grade = calcGrade(asg1, asg2, exam)
        print "%s %s, Grade = %s" % (fname, lname, grade)
        line = f.readline()
    f.close()

main()

this is what i have so far

with

Fred, Nurke, 16, 17, 22
Jack, Sprat, 12, 13, 19

being in a separate input file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.