I'm working on a project where I need to take an existing .txt file containing student names and number grades, assign letter grades to each student, and output name, grade, letter grade separated by commas. Then send the new output to a new file.
Example of text:
name_one, 85
name_two, 76.5
Desired Output:
name_one, 85, B
name_two, 76.5, C
Here's the code I have so far:
import numbers
import string
gradesDict = {}
with open("C:\\Users\\awolf\\.vscode\\extensions\\StudentExamRecords.txt", "r") as f:
for line in f:
(key, value) = line.strip().split(",")
gradesDict[float(value)] = value
def letter_grade(value):
if value > 95:
return "A"
elif value > 91:
return "A-"
elif value > 87:
return "B+"
elif value > 83:
return "B"
elif value > 80:
return "B-"
elif value > 78:
return "C+"
elif value > 75:
return "C"
elif value > 70:
return "D"
else:
return "F"
print(key + "," + value)
I've created a dictionary to contain the data, opened the file path, stripped line notations and split string values into key/value, converted values to float, and created conditions to assign letter grades. What I am getting hung up on is adding the letter grade to the end of each line. I feel like I need to create a new variable "letterGrade" within the "def letter_grade" function and append the information to each line, but nothing I have tried is working.