Here is my code:
I can get it to run but it will not write to the outfile!
I could also use some help with my functions. The first one works but the other three do not!
Code:
def calcaverage (grades):
return float(sum(grades)/len(grades))
#def findHighest(grades):
sort(grades)
return grades[-1]
#def findLowest(grades):
sort(grades)
return grades[0]
#def findLongestName(names):
pass
infile = open("names.txt", "r")
names = []
grades = []
is_name = True
individual_grades = []
individual_average = []
for line in infile:
if is_name == True:
names.append(line.strip())
is_name = False
else:
num = int(line)
if num > 0:
individual_grades.append(num)
else:
is_name = True
grades.append(individual_grades)
average= float(sum(individual_grades)/len(individual_grades))
if average > 89:
average = "A"
elif average > 79:
average = "B"
elif average > 69:
average = "C"
elif average > 59:
average = "D"
else:
average = "F"
#grades.append(average)
individual_grades = []
for n, g in zip (names, grades):
average = calcaverage(g)
#high = findHighest(g)
#low = findLowest(g)
print(grades)
print (names)
print(average)
outfile = open("namesgrades.txt", "w")
for n, g, i in zip(names, grades, individual_average):
outfile.write("%s" % names)
outfile.write("%f" % grades)
outfile.write("%s" % individual_average)
outfile.close()
I could also use some help with my functions. The first one works but the other three do not!
Comment