473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why won't this write to an outfile?

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
3 2749
dwblas
626 Expert 512MB
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
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
dwblas
626 Expert 512MB
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

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

Similar topics

14
by: Eli | last post by:
I've got a script that I'm trying to debug which uses document.write() to place HTML within a page. In both IE6 and Firefox when I view source, I see only the script itself and not any HTML as...
3
by: John Hoffman | last post by:
Hello I have a routine that among other things, reads and writes some simple settings to the registry. Everything works fine. I port this program to a windows service and start it and everthing...
2
by: Dave Griffin | last post by:
This is a followup to my question in doing unmanaged DLLs in .NET. I was able to create the DLL and the application was able to see it. So far so good. Now I need to make that unmanaged DLL read an...
4
by: Phil | last post by:
I've made a command line app. two in fact. one in dotnet and the other vb6 to see if that made a dif. The commandline app writes to a file. this works fine when I run it from windows. If I run it...
0
by: TheClair | last post by:
Hi, I'm trying to install php5 as a shared module for Apache 2 (on SuSe 10.0). When I compile I don't get libphp5.so. I don't know why, I'm not getting any errors, there are a few warnings, but...
7
by: rhitz1218 | last post by:
All: I'm trying to create a log file. I now I can be able to do that by doing the following: FILE *outfile; outfile = fopen("log.txt", "w"); fprintf("This is the text");
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
2
by: =?Utf-8?B?UGV0ZQ==?= | last post by:
Hi, I have developed a VB application in VS2005 and deployed it sucessfully to XP using an MSI file built with a standard VS setup project. The MSI creates some keys under...
4
by: hausburn | last post by:
The Thankyou(); function will not print the variables fname and lname to screen. the variables are extracted from a file. #include <cstdlib> #include <iostream> #include <fstream> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.