python - Calculating average grade wont succeed -
i have calculate average grade of person in python. received input file , in combination that, have calculate everyones average grade. tried lot, got average grade of first person.. can me?
the input file following:
tom bombadil__________6.5 5.5 4.5 dain ijzervoet________6.7 7.2 7.7 thorin eikenschild____6.8 7.8 7.3 meriadoc brandebok____1.0 5.0 7.7 sam gewissies_________2.3 4.5 6.7
the output follow:
tom bombadilhas average grade of 5.5 dain ijzervoethas average grade of 5.5 thorin eikenschildhas average grade of 5.5 meriadoc brandebokhas average grade of 5.5 sam gewissieshas average grade of 5.5
i used code:
def names(lines): in lines: invoer_split = i.split("_") first_name = invoer_split[0] print first_name + "has average grade of %.1f" %(average_grade(names)) def average_grade(names): in lines: grades_split = i.split("_") grades = grades_split[-1] grades_float = map(float,grades.split()) grades_average = sum(grades_float)/3 return grades_average grades_file = open('grades1+2.in') lines = grades_file.readlines() names(lines) average_grade(names)
instead of processing names , grades separately, them @ same time. change input dictionary mapping strings float.
with open('filename') f: grades_dict = {} line in f: name, *underscores, grades = line.split('_') #i forget when syntax became thing. might have assign these separately grades = list(map(float, grades.split())) grades_dict[name] = sum(grades)/len(grades) name, grade in grades_dict.items(): print('{0} has avergae grade of {1}'.format(name, grade))
Comments
Post a Comment