Question: Hello, How to fix the following code in Python that meet the following requirements: (1) ask the user for the name of an input file,

Hello,

How to fix the following code in Python that meet the following requirements:

(1) ask the user for the name of an input file, then open this input file using the name by the user.

(2) ask the user for the name of an output file, then open this output file using the name provide by the user.

(3) process the grade data

(4) close both the input and output file

You can turn this into a main function like this:

def main(): # The open_the_input_file function needs to be defined by you. It should # ask the user for the name of an input file, then open this input file using # the name provided by the user. The function should return the open file. input_file = open_the_input_file() # The open_the_output_file function needs to be defined by you. It should # ask the user for the name of an output file, then open this output file using # the name provided by the user. The function should return the open file. output_file = open_the_output_file() # This function also need to be defined by you. It should read in data from the # input file, figure out the correct letter grade for the data that is read from # the input file, then write the letter grade data to the output file. process_grade_data(input_file, output_file) # The final step is to close your files. input_file.close() output_file.close() # Then call main to run your program. main()

During the 3rd step in my outline above ("process the grade data") your program should use a loop to read in data from the input file. That input file is what contains the names of the students, the student types, and the scores. This data should be read in from the file, not "hard coded" in your program.

In addition, please use my structure of the program as I shared in this post. It will make your life much easier! All code should be inside one of your functions except for the one call to main() at the very end of the program.

this is the code:

def letter_grade(grade, is_grad): if is_grad: if 95 <= grade <= 100: return 'H' elif 80 <= grade <= 94: return 'P' elif 70 <= grade <= 79: return 'L' elif 0 <= grade <= 69: return 'F' else: if 90 <= grade <= 100: return 'A' elif 80 <= grade <= 89: return 'B' elif 70 <= grade <= 79: return 'C' elif 60 <= grade <= 69: return 'D' elif 0 <= grade <= 59: return 'F' def assign_grade(score, student_type, curve_score=None): # If curve_score is provided, curve the score before assigning a letter grade if curve_score is not None: score = curve_score + (score - curve_score) * 100 / (100 - curve_score) # Assign the letter grade return letter_grade(score, student_type == 'GRAD') # Prompt the user for the input and output file names input_filename = input('Please enter the name of the input file: ') output_filename = input('Please enter the name of the output file: ') # Prompt the user if they want to curve the grades curve_grades = input('Would you like to curve the grades? (Y/N) ').lower() == 'y' curve_score = None # If curving the grades, prompt the user for the curve score if curve_grades: curve_score = float(input('Please enter the score that should map to a "100%" grade: ')) # Process the input file and write the output file try: with open(input_filename, 'r') as input_file, open(output_filename, 'w') as output_file: for line in input_file: # Read the student type, name, and score from the input file student_type = line.strip() name = input_file.readline().strip() score = float(input_file.readline().strip()) # Assign the letter grade try: letter_grade = assign_grade(score, student_type, curve_score) except ValueError as e: print('Error processing data for student {}: {}'.format(name, str(e))) print('Please fix the errors and try again.') break # Write the output to the output file output_file.write(name + ' ') output_file.write(letter_grade + ' ') print("Unknown student category detected 'HIGH_SCHOOL'.") except FileNotFoundError: print('All data was successfully processed and saved to the requested output file.') except Exception as e: print('An error occurred while processing the input file: {}'.format(str(e))) print('Please fix the error and try again.'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!