USING PYTHON: Write a function named input_roster() that prompts a user for the
ID: 674634 • Letter: U
Question
USING PYTHON:
Write a function named input_roster() that prompts a user for the name of a file containing a class roster. The function has no parameters. This roster file has names in the format “last_name,first_name ” (no spaces). Your function should read this file and create a new roster file with the format “first_name last_name ” (one space between the names and no comma). Your program MUST be written to prompt the user for the name of the roster file to be read. The new roster that your program creates MUST be output to a file named roster_first_last.txt. (Note: there are no spaces in this filename.)
To help you test your program, a sample class roster named roster_last_first.txt is provided as an attachment to this assignment. Test your program by running it on this sample roster and several different rosters that you make up. Verify that the output file looks correct by opening it with an editor (e.g., TextEdit, vim, WordPad, Notepad++…). No assertEqual() tests are expected for this function. The TAs will run your program with a larger test file to verify that the program works correctly.
Explanation / Answer
name = input("Please enter the name of a file containing a class roster: ") f = open(name,'r') fout = open("roster_first_last.txt",'w') for line in f.readlines(): line = line.split(',') line[1]=line[1][0:-1] fout.write(line[1]+" "+line[0]+' ')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.