i am writing a program that reads the file i create. The file has grades and weights of grades anddd 3 person but i need to create a program that will read the information from the file, calculate and print the total average of each student given by the formula wn*gn/100 and then totals the average of all grades. my file looks like

FirstName lastName w1 g1 w2 g2 w3 g3 w4 g4
Billy Bother 20 89 30 94 50 82
Hermione Heffalumo 40 93 60 97
Kurt Kidd 20 88 30 82 40 76 10 99

and so far i did

import math

FileName = input("Enter the name of the file")

infile = open("grades.txt","r")

contents = infile.read()
lines = contents.split("\n")

sum = 0
for i in range(0,len(lines)):
    line = lines[i]
    words = line.split(" ")
    sum = sum+(words[0])
    avg =((wn+gn)/100)
    print(sum/len(lines))
    print(avg)


total_avg = 0
for i in range(3):
    total_avg = avg[i]
    total_avg= total_avg + avg[i]

print("total_avg", total_avg)

but doesnt work :/

the out put should be somethin like this

Billy Bother's average: 87.0
Hermione Heffalump's average: 95.4
Kurt Kidd's average: 82.5
Class average: 88.3


THANKS !

Recommended Answers

All 12 Replies

Do not index lines but iterate over the lines by for. Code I can not check deeper, as indention is off because of missing code-tags

Python is indention sensitive.

you cant read my code ?

help ? lol

sum shadows the built in function, do not do that, use other name. It is better to actually use the builtin sum instead of reinventing the wheel. The variables at line 15 are uninitialized. You also can not add string to integer. Add print statements before the error. Remember also to check type of variables in case of type errors like line 14. You should repost the fixed code and error messages in case you get stuck. You might like to read some ideas on using DaniWeb from post linked in my signature.

the average is calculated like this( w1*g1 +w2*g2..... wn*gn)/100 i dont know how to indicade it on my code so i just said (wn*gn)/100 but doesnt work it should work for n times grades and weight so even if i have 4 peoples grade the code should know its 4 by reading the file :/

You have to define w1...wn, and g. They should be in the list "words". You can see what is in "words" with this code. Note that arithmetic only works with numbers, not strings. See this page for starters.

#FileName = input("Enter the name of the file")     ## not used by the program
 
infile = open("grades.txt","r")
 
 
sum = 0
for line in infile:
    words = line.split(" ")
    for ctr in range(len(words)):
        print ctr, words[ctr], type(words[ctr])

Have you learned to make functions? How you would process numbers from strings in line you split?

nooo :/

by the way this is phyton. ctr doesnt work i think

so im i suppose to do weight = [w1...wn]
grades = [g1...gn]
to define it ? then avg = (weight + grades)/100 ???

It is better to actually use the builtin sum instead of reinventing the wheel.

I do not know what you mean by this can you give an example ? so far i did

import math

FileName = input("Enter the name of the file")

infile = open("grades.txt","r")

contents = infile.read()
lines = contents.split("\n")

def main():
    sum = 0
    words = [w1*g1,...,wn*gn]


for i in range(0,len(lines)):
    line = lines[i]
    words = line.split(" ")
    print(words)
    sum = sum + words[0]
    avg =(words/100)
    print(avg)
    print(sum/(len(lines-1)))

i created a list and defined words but like you said there is a problem on sum = sum +words[0] i dont know how to fix that

# math never used
import math

FileName = input("Enter the name of the file") # never used

infile = open("grades.txt","r")

lines = infile.readlines()

# this funcition is never called, it should be in beginning of file anyway
def main():
    # sum is builtin function, replacing it with local variable in this function
    # is confusing
    sum = 0
    # w1 etc not defined and  ,..., is bad syntax
    #words = [w1*g1,...,wn*gn]

# main routine restarts 
for i in range(0,len(lines)):
    line = lines[i]
    words = line.split(" ")
    print(words[0])
    #type Error, can not add first name to integer
    #sum = sum + words[0]
    # type error, words in list of strings
    #avg =(words/100)
    #print(avg)
    # global variable sum uninitialized, can not decrease list of string by integer
    #print(sum/(len(lines-1)))

See comments I left functioning lines commented out unfunctioning ones you should delete, here new start:

from __future__ import print_function

with open("grades.txt") as infile:
    for line in infile:
        words = line.rstrip().split(" ")
        print(words[0:2])
        name, values = words[0:2], words[2:]
        if not(values[0].isdigit()):
            print('Header discarded:', line)
            continue

        # TODO: here prepare the values from string to integers by using the int-function

        # uninterleave with slicing every secon
        g, w = values[::2], values[1::2]
        print(' '.join(name),'has', len(g), 'grades')
        print()
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.