def determineHours(): with open ("StudyHours.txt", "r") as file1: data = file1.readlines() total_study_hours = 0 for i, line in enumerate(data): if "error" in line: print(f"Error found in line {i + 1}: {line}") correct = input("Do you want to correct this line?(y\n)") if correct == "y": new_line = input("Enter corrected line:") data[i]=new_line for record in data: fields = record.strip().split(',') name = fields[0].title() credits = int(fields[1]) grade = fields[2] if grade == 'A':#determine weekly study hours based on desired grade study_hours = 15 * credits elif grade == 'B': study_hours = 12 * credits elif grade == 'C': study_hours = 9 * credits elif grade == 'D': study_hours = 6 * credits elif grade == 'F': study_hours == 0 else: print("Invalid grade for student {'name'}") with open ("HowManyHours.txt", 'a') as write_file: write_file.write(f"{name},{credits},{study_hours},{grade}\n") ******Here is my code, I will attach the errors I am recieving if you wouldnt mind helping me figure these out, I would appreciate it. I believe it is saying that the index field I am trying to assign isnt valid. I will also attach the file it is pulling from so you can view. The program will READ in data from a text file named StudyHours.txt. The user corrects any bad data. The program updates the information in StudyHours.txt file. For example if the file contains a letter grade of K which is not a possible letter grade. You will create and submit a text file with a minimum of 5 additional records from example below. The file is named StudyHours.txt and contains the following format: first line full name second line number of credits third line grade desired for each class
def determineHours():
with open ("StudyHours.txt", "r") as file1:
data = file1.readlines()
total_study_hours = 0
for i, line in enumerate(data):
if "error" in line:
print(f"Error found in line {i + 1}: {line}")
correct = input("Do you want to correct this line?(y\n)")
if correct == "y":
new_line = input("Enter corrected line:")
data[i]=new_line
for record in data:
fields = record.strip().split(',')
name = fields[0].title()
credits = int(fields[1])
grade = fields[2]
if grade == 'A':#determine weekly study hours based on desired grade
study_hours = 15 * credits
elif grade == 'B':
study_hours = 12 * credits
elif grade == 'C':
study_hours = 9 * credits
elif grade == 'D':
study_hours = 6 * credits
elif grade == 'F':
study_hours == 0
else:
print("Invalid grade for student {'name'}")
with open ("HowManyHours.txt", 'a') as write_file:
write_file.write(f"{name},{credits},{study_hours},{grade}\n")
******Here is my code, I will attach the errors I am recieving if you wouldnt mind helping me figure these out, I would appreciate it. I believe it is saying that the index field I am trying to assign isnt valid. I will also attach the file it is pulling from so you can view.
The program will READ in data from a text file named StudyHours.txt. The user corrects any bad data. The program updates the information in StudyHours.txt file. For example if the file contains a letter grade of K which is not a possible letter grade. You will create and submit a text file with a minimum of 5 additional records from example below. The file is named StudyHours.txt and contains the following format:
first line full name
second line number of credits
third line grade desired for each class
Step by step
Solved in 4 steps with 2 images