J
jstreet10
How would i use function statements instead of loops(for) in this code? i
have tried some, but not working.
# this program is suppose to read fm a file, computes the average and
reads fm a file
# file grade info: dave, 75; david, 70; davy, 60; john, 70; jon, 60; nina,
55; nino, 50; ray, 65; sherry, 90; terry, 85
import os, sys, string
student_table = {}
grade_counter = 0
input_file = open("grades.txt","r")
sum = 0
for record in input_file.readlines():
grade_counter += 1
record_key , record_value = string.split(record,",")
student_table[record_key] = record_value
sum += int (record_value)
average = sum/grade_counter
input_file.close()
student_names = student_table.keys()
student_names.sort()
output_file = open("processed.txt", "w")
for student_name in student_names:
output_file.write(student_name +"," + student_table
[student_name])
output_file.write("Average grade is" + str(average))
output_file.close()
have tried some, but not working.
# this program is suppose to read fm a file, computes the average and
reads fm a file
# file grade info: dave, 75; david, 70; davy, 60; john, 70; jon, 60; nina,
55; nino, 50; ray, 65; sherry, 90; terry, 85
import os, sys, string
student_table = {}
grade_counter = 0
input_file = open("grades.txt","r")
sum = 0
for record in input_file.readlines():
grade_counter += 1
record_key , record_value = string.split(record,",")
student_table[record_key] = record_value
sum += int (record_value)
average = sum/grade_counter
input_file.close()
student_names = student_table.keys()
student_names.sort()
output_file = open("processed.txt", "w")
for student_name in student_names:
output_file.write(student_name +"," + student_table
[student_name])
output_file.write("Average grade is" + str(average))
output_file.close()