0

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)

Edited by my_py: n/a

2
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by my_py
0

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

Edited by pyTony: n/a

0

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...

0

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...

0

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.

Edited by pyTony: n/a

0

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!

Edited by my_py: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.