def determine_grade(): try:#creating this block to handle any exceptions that might occur when trying to convert. with open("Grades.txt", "r") as file:#open Grades to read data with open ("HowManyHours.txt", "a") as writeFile:#append data to HowManyHours grade_map = {15:"A",12:"B",9:"C",6:"D",0:"F"}#defining the dictionary for line in file: line_list = line.strip().split(",")#splitting lines into a list name = line_list[0].title() credits = int(line_list[1]) study_hours = int(line_list[2]) grade = grade_map.get((Study_hours / credits), "invalid") if (credits > 0) and (credits <= 55) and (0 <= study_hours <= 168) and (grade != "Invalid"):#validating data to make sure it is within range writeFile.write("Full Name: " + name.capitalized() + "\n")#if data is validated it will write the data to HowManyHours.txt writeFile.write("Grade: " +grade+ "\n") writeFile.write("Credits: " +str(credits) + "\n") writeFile.write("Weekly Study Hours: " +str(study_hours) + "\n") elif (credits < 0) or (credits > 55): print("You entered " +str(credits) + "which is an incorrect value for credits. Try again.") credits = int(input("Please enter the number of credits you are taking such as 3 for 3 credits.")) grade = grade_map.get((study_hours / credits), "Invalid") writeFile.write("Full Name:" +name.capitalized()+"\n") writeFile.write("Grade:" + grade + "\n") writeFile.write("Credits:" +str(credits) + "\n") writeFile.write("Weekly Study Hours:" + str(study_hours) + "\n") else: print("Invalid data format") except ValueError: print("Invalid data format. Please make sure the credits and study hours are integers.") file.close() writeFile.close() Traceback (most recent call last): File "C:\Users\User\OneDrive\Desktop\F\FinalProject.py", line 191, in choice_option[ch]() File "C:\Users\User\OneDrive\Desktop\F\FinalProject.py", line 120, in determine_grade credits = int(line_list[1]) IndexError: list index out of range Please help with the errors. Attached is the txt files the program is pulling from.
def determine_grade():
try:#creating this block to handle any exceptions that might occur when trying to convert.
with open("Grades.txt", "r") as file:#open Grades to read data
with open ("HowManyHours.txt", "a") as writeFile:#append data to HowManyHours
grade_map = {15:"A",12:"B",9:"C",6:"D",0:"F"}#defining the dictionary
for line in file:
line_list = line.strip().split(",")#splitting lines into a list
name = line_list[0].title()
credits = int(line_list[1])
study_hours = int(line_list[2])
grade = grade_map.get((Study_hours / credits), "invalid")
if (credits > 0) and (credits <= 55) and (0 <= study_hours <= 168) and (grade != "Invalid"):#validating data to make sure it is within range
writeFile.write("Full Name: " + name.capitalized() + "\n")#if data is validated it will write the data to HowManyHours.txt
writeFile.write("Grade: " +grade+ "\n")
writeFile.write("Credits: " +str(credits) + "\n")
writeFile.write("Weekly Study Hours: " +str(study_hours) + "\n")
elif (credits < 0) or (credits > 55):
print("You entered " +str(credits) + "which is an incorrect value for credits. Try again.")
credits = int(input("Please enter the number of credits you are taking such as 3 for 3 credits."))
grade = grade_map.get((study_hours / credits), "Invalid")
writeFile.write("Full Name:" +name.capitalized()+"\n")
writeFile.write("Grade:" + grade + "\n")
writeFile.write("Credits:" +str(credits) + "\n")
writeFile.write("Weekly Study Hours:" + str(study_hours) + "\n")
else:
print("Invalid data format")
except ValueError:
print("Invalid data format. Please make sure the credits and study hours are integers.")
file.close()
writeFile.close()
Traceback (most recent call last):
File "C:\Users\User\OneDrive\Desktop\F\FinalProject.py", line 191, in <module>
choice_option[ch]()
File "C:\Users\User\OneDrive\Desktop\F\FinalProject.py", line 120, in determine_grade
credits = int(line_list[1])
IndexError: list index out of range
Please help with the errors. Attached is the txt files the program is pulling from.
Step by step
Solved in 5 steps