I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n'
Below are the assignment details:
This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a whole number). The fields are separated by a space.
It calculates the student’s letter grade as follows:
It writes to an output file named grade_output.txt. The output file is similar to the input file, but has the student’s letter grade at the end of each line.
The program also calculates and displays the number of file records and the class average grade, and then lists the entire contents of the output file.
Here is a pseudocode definition of the program requirements:
Determine the full file names (including the folder) for grade_input.txt and grade_output.txt
Open grade_input.txt for input
Open grade_ output.txt for output
Initialize variables for counting records and computing average grade
For each record in the input file
Read a line of data for that student
Split the line into its individual fields
Store the individual fields into variables (first name, last name and grade)
Calculate the letter grade
Write data for that student to the output file
Update variables used to count records and compute average grade
Close the input and output files
If the record count is 0
Display “no records in input file”
Else
Display the record count
Calculate and display the average grade using 1 fractional digit
List the contents of the output file
Some of code is already written for you. Download the GradeCalculator.py script starting file. It includes comments where you have to add your code.
Download the input file grade_input.txt. Put it in the same folder as the GradeCalculator.py. You can view the file in a simple text editor like notepad.
Make the necessary changes to the GradeCalculator.py script.
Test your program to make sure that it runs and produces correct results. Look at the output file in a text editor to make sure that it is correct.
Here is a sample test run (your file folders will be different):
grade calculator
This script file path is C:\ch04\GradeCalculator.py
This input file path is C:\ch04\grade_input.txt
This output file path is C:\ch04\grade_output.txt
6 records in the input file
average class grade 87.0
grade file contents:
Roger Jones 75 C
Maria Torres 90 A
Tong Nquyen 92 A
Rick DiPersio 100 A
Sarah Stevens 80 B
Edith Hills 85 B
Program:
# opening input.txt file for reading purpose f1 = open('input.txt') # opening output.txt file for writing purpose f2 = open('output.txt', 'w') # NOTE: Here all files are in same directory as of program file(GradeCalculator.py) # initializing variables for record count and average grade rec_count = 0 avg_grade = 0 # for each record in input file for record in f1.readlines(): # incrementing record count rec_count += 1 # splitting values in 3 different variables first_name, last_name, grade = record.split() # converting str grade to int grade grade = int(grade) # adding grade to avg grade avg_grade += grade # calculating letter grade if grade >= 90: l_grade = 'A' elif 90 > grade >= 80: l_grade = 'B' elif 80 > grade >= 70: l_grade = 'C' elif 70 > grade >= 60: l_grade = 'D' else: l_grade = 'F' # writing data for student in output file f2.write(first_name+" "+last_name+" "+str(grade)+" "+l_grade+"\n") # closing input and output files f1.close() f2.close() # printing output print("grade calculator") # NOTE: Here file paths given in question are printed to match with output # These paths should be replaced with your own ones print("This script file path is C:\\ch04\\GradeCalculator.py") print("This input file path is C:\\ch04\\grade_input.txt") print("This output file path is C:\\ch04\\grade_output.txt") # if record count is 0, print no records if rec_count == 0: print("no records in input file") # else print record count else: print(rec_count, "records in the input file") # calculating average count and printing with 1 fractional digit avg_grade = avg_grade/rec_count print("average class grade", round(avg_grade, 1)) # opening output.txt in reading mode to print records f = open('output.txt') print("grade file contents:") # printing records, with stripping newlines for record in f.readlines(): print(record.strip()) # closing opened file f.close()
Code Snippet:
Output Snippet:
input.txt:
output.txt:
I hope you got the provided solution.
Thank You.
Get Answers For Free
Most questions answered within 1 hours.