I have written almost all of my program, except I am stuck on this certain part. I need to write an averege out to figure all students final grades out as a statistic of the course. Students name and final grade have been appended to an external file(keep in mind more students and grades can be appended)...here is what I have so far, any input is highly appreciated if my code is wrong please don't hesitate in letting me know. Thank you.

fname = input("What is the file name?")
    file1 = open(fname,'r')
    sum = 0.0
    count = 0
    for line in file1:
        sum = sum + eval(line)
        count = count + 1

    print("\nThe average of the numbers is", sum/count)

Recommended Answers

All 6 Replies

Your indention is off. Do you really want to hide built in function sum with your variable? Eval is unsafe from user input.

Your indention is off. Do you really want to hide built in function sum with your variable? Eval is unsafe from user input.

I don't understand without seeing code, sorry

here is the way my external file is formatted for example
tom jones, 100,
cindy smith, 88,
and so on...

You must then split the lines with split method.

Your indention is off. Do you really want to hide built in function sum with your variable? Eval is unsafe from user input.

I don't understand without seeing code, sorry I've tried

sum = sum + int(line.split(',')[1])

and it just doesn't work

here is the way my external file is formatted for example
tom jones, 100,
cindy smith, 88,
and so on...

You must separate only numeric part with indexing. First put print statement instead of formula and when your program prints correct part of line then put it to sum formula.

Actually your formula looks correct, but maybe you have empty line which causes problem. Try

if ',' in line:

before formula.

You must separate only numeric part with indexing. First put print statement instead of formula and when your program prints correct part of line then put it to sum formula.

Actually your formula looks correct, but maybe you have empty line which causes problem. Try

if ',' in line:

before formula.

pyTony, I just wanted to say thanks for being patient and kind to me and actually offering help...I've asked several people and all I have gotten is arrogant, better than thou answers - so to you I say thanks again!

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.