By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Why won't this write to an outfile?

P: 23
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. def calcaverage (grades):
  2.     return float(sum(grades)/len(grades))
  3. #def findHighest(grades):
  4.     sort(grades)
  5.     return grades[-1]
  6. #def findLowest(grades):
  7.     sort(grades)
  8.     return grades[0]
  9. #def findLongestName(names):
  10.     pass
  11.  
  12. infile = open("names.txt", "r")
  13. names = []
  14. grades = []
  15. is_name = True
  16. individual_grades = []
  17. individual_average = []
  18. for line in infile:
  19.     if is_name == True:
  20.         names.append(line.strip())
  21.         is_name = False
  22.     else:
  23.         num = int(line)
  24.         if num > 0:
  25.             individual_grades.append(num)
  26.         else:
  27.             is_name = True
  28.             grades.append(individual_grades)
  29.             average= float(sum(individual_grades)/len(individual_grades))
  30.             if average > 89:
  31.                 average = "A"
  32.             elif average > 79:
  33.                 average = "B"
  34.             elif average > 69:
  35.                 average = "C"
  36.             elif average > 59:
  37.                 average = "D"
  38.             else:
  39.                 average = "F"
  40.             #grades.append(average)
  41.             individual_grades = []
  42. for n, g in zip (names, grades):
  43.     average = calcaverage(g)
  44.     #high = findHighest(g)
  45.     #low = findLowest(g)
  46. print(grades)    
  47. print (names)
  48. print(average)
  49. outfile = open("namesgrades.txt", "w")
  50. for n, g, i in zip(names, grades, individual_average):
  51.     outfile.write("%s" % names)
  52.     outfile.write("%f" % grades)
  53.     outfile.write("%s" % individual_average)
  54. outfile.close()
  55.  
  56.  
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!
Nov 30 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 624
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.
Dec 1 '10 #2

P: 23
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!
Dec 1 '10 #3

Expert 100+
P: 624
Do you really want to write the complete contents of names, grades, and individual_average to the file many times, i.e. every pass through the for() loop.
Expand|Select|Wrap|Line Numbers
  1. for n, g, i in zip(names, grades, individual_average):
  2.     outfile.write("%s" % names)
  3.     outfile.write("%f" % grades)
  4.     outfile.write("%s" % individual_average) 
Dec 2 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.