Purpose: To practice writing data to a file Write a function called write_score_
ID: 3798340 • Letter: P
Question
Purpose: To practice writing data to a file
Write a function called write_score_file that takes two arguments, a string representing the name of a file to be written, and the response database (a dictionary with all the student information in it). Your function should open the named file for writing, and then write the student scores, one per line, as in the following example:
10021795,Samden Cross,25
10975851,Dannae Evenstar,21
10461276,Sanna Tunna,16
Hint: To build this function, it is probably a good idea to ignore the file until you know the data has the correct format. To do this, simply use print instead of write. When the data has the right format, then have the function open the file, write to it, and then close it.
Before going on, you want to be sure that your function works properly. Add the following testing code to your program:
When you open up the file score_file_example.txt, you should see something like this:
345,xyzzy,3
123,foo,3
234,bar,4
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
(This is Python 3.5)
Explanation / Answer
Here is the code for you:
def write_score_file(fileName, response_db):
outF = open(fileName, 'w') #Opens a file in write mode
for i in range(len(response_db)):
outF.write(response_db[i])
outF.write(' ')
outF.close()
To test your code, we also need the other functions like grade(), index_class(), indexed_class(). So, unable to test it. If you need any modifications, just get back to me.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.