Why won't this write to an outfile?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anderson1373
    New Member
    • Nov 2009
    • 23

    #1

    Why won't this write to an outfile?

    Here is my code:
    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 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!
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    There is no way to tell without the complete error message. Some possibilities:
    1. There was a problem opening the file, and since it is not open you can not write to it
    2. You don't have write permission for that file or directory
    3. You tried looking in the file before it was closed/buffers were flushed.

    See the last post in your other thread for an idea on how you find the lowest or highest in a list.

    Comment

    • anderson1373
      New Member
      • Nov 2009
      • 23

      #3
      It did not give an error message! It ran the program correctly and printed to the screen correctly but it didnt write it to the outfile!

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        Do you really want to write the complete contents of names, grades, and individual_aver age to the file many times, i.e. every pass through the for() loop.
        Code:
        for n, g, i in zip(names, grades, individual_average):
            outfile.write("%s" % names)
            outfile.write("%f" % grades)
            outfile.write("%s" % individual_average)

        Comment

        Working...