Jan-18-2021, 02:25 PM
I have a .txt file with names and their school grades.
i wrote a program to output the name of a student + his/her average grade.
I find my approach doing this somehow unefficient and i fell like could be done better in a different way.
My Code:
thanks!
i wrote a program to output the name of a student + his/her average grade.
I find my approach doing this somehow unefficient and i fell like could be done better in a different way.
My Code:
f = open('python_ist_cool_tabelle.txt', 'r') """ The .txt file: joe 10 15 20 30 40 bill 23 16 19 22 sue 8 22 17 14 32 17 24 21 2 9 11 17 grace 12 28 21 45 26 10 john 14 32 25 16 89 """ a = f.readlines() new_list = [] names_list = [] grade_averages = [] # creating seperate lists for names and grades for i in a: items = i.split() grades = items[1:] names = items[0] new_list.append(grades) names_list.append(names) # calculating the average of someones grades for k in new_list: sum = 0 for j in k[0:]: #print(int(j)) sum = sum + int(j) average = sum / len(k) grade_averages.append(average) # combining names and average grades for q in range(len(names_list)): print(names_list[q] + ': your average score is ' + str(grade_averages[q]))The Output:
joe: your average score is 23.0 bill: your average score is 20.0 sue: your average score is 16.166666666666668 grace: your average score is 23.666666666666668 john: your average score is 35.2if you could read through my code and tell me what i could better would help me alot!
thanks!
