This is the code I have written for my final proect. I am running into problems with the menu, I am thinking maybe I have it indented wrong somewhere or something. When I run the program it pops up A is not defined when it clearly is. I am not sure what it meant. This is the way I did the last menu in the las menu driven program I coded and it worked fine. Please help, answer in Python only. I am going to take out a lot of the code so I can submit the question but will leave what I think i relevant to my problem. My menu options are the functions determineHours, determine_grade, and display_averages_and_totals. You will see how I have it coded at the bottom but it isnt working please help. SO AGAIN, I TOOK A LOT OF CODE OUT TO MAKE THIS QUESTION FIT but it doesnt seems to be selecting the options correctly, it says A is not defined.
This is the code I have written for my final proect. I am running into problems with the menu, I am thinking maybe I have it indented wrong somewhere or something. When I run the program it pops up A is not defined when it clearly is. I am not sure what it meant. This is the way I did the last menu in the las menu driven program I coded and it worked fine. Please help, answer in Python only. I am going to take out a lot of the code so I can submit the question but will leave what I think i relevant to my problem.
My menu options are the functions determineHours, determine_grade, and display_averages_and_totals. You will see how I have it coded at the bottom but it isnt working please help. SO AGAIN, I TOOK A LOT OF CODE OUT TO MAKE THIS QUESTION FIT but it doesnt seems to be selecting the options correctly, it says A is not defined.
lines = file
def determineHours():
def correct_errors(data):
if "error" in line:#check if there are errors
print(f"Error found in line {i + 1}: {line}")
correct = input("Do you want to correct this line? (y/n)")#give user option to correct any errors
if correct == "y":
new_line = input("Enter corrected line:")
data[i] = new_line
with open ("StudyHours.txt", "r") as file1:
data = file1.readlines()
correct_errors(data)#reading in data and correcting any errors.
total_study_hours = 0
def to_proper_case(name):#defining to_proper_case function
return name.title()
for record in data:
fields = record.strip().split(',')
name = fields[0]
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 {record['name']}")
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.split()#splitting lines into a list
file.close()
writeFile.close()
def display_averages_and_totals(self):
try:
with open ("HowManyHours.txt", "r") as file:#read data from file
data = file.readlines()
except FileNotFoundError:
raise FileNotFoundError("HowManyHours.txt is not found.")
total_credits = 0
total_study_hours = 0
total_students = 0
for line in data:
values = line.strip().split(",")#using a delimiter to split the line into a list of values
student = values[0]
total_credits += int(values[1])
total_study_hours += int(values[2])
grade = values[3]
total_students += 1#calculating total students
average_credits = total_credits / total_students#calculating average credits.
average_study_hours = total_study_hours / total_students#calculating average study hours.
if __name__ == '__main__':
print('\t\t\tMenu')#main menu
print('A: Determine Hours to Study:')
print('B: Determine Grade:')
print('C: Display Averages and Totals:')
print('D: Quit Program')
ch= input('\nEnter your menu choice:').upper()
choice_option = {#menu options the user gets to choose from
A: determineHours,
B: determine_grade,
C: display_averages_and_totals,
D: quit
}
choice_option[ch]()
print("Thank you for using the Grade Calculator 2.0. Your support allows us to continue making apps you will love. Thank you.")
Trending now
This is a popular solution!
Step by step
Solved in 3 steps