Question
I thought i had defined the ch variable I have in my code ( the error is referencing the menu code at the bottom.) It
I thought i had defined the ch variable I have in my code ( the error is referencing the menu code at the bottom.) It is throwing me an error saying ch hasnt been defined but i thought i defined it by doing this...
ch= input('\nEnter your menu choice:').upper()
Please help me with this code. This is one of my last errors and I am almost done. How do I define it in the choice_option dictionary code wise to make this error disappear?
Traceback (most recent call last):
File "C:\Users\User\OneDrive\Desktop\F\FinalProject.py", line 210, in
choice_option[ch]()
#Shonda Deese
#1/25/2023
#COP1000FINALPROJECT
print('Welcome user to the Grade Calculator Version 2.0. I thank you for continuing support for this program. This allows us to continue focusing on making apps you will love. Thanks.Programmer:Shonda Deese')
total_credits = 0
total_study_hours = 0
total_students = 0
###creating a class for student information
##class Student:
## def __init__(self, name, employee_id, department, credits, desired_grade):
## self.name = name
## self.employee_id = employee_id
## self.department = department
## self.credits = credits
## self.desired_grade = desired_grade
## self.study_hours = MinHours[desired_grade] * credits
##
##
## def __str__(self):
## return f"{self.name}, Credits: {self.credits}, Study Hours: {self.study_hours}, Grade: {self.desired_grade}"
#creating class to represent the report with all desired information.
class Report:
def __init__(self, name, employee_id, department):#, report_for, report_employee_id, report_department):
self.name = name
self.employee_id = employee_id
self.department = department
## self.students = []
## self.report_for = report_for
## self.report_department = report_department
## self.report_employee_id = report_employee_id
## def add_student(self, student):#adds a student to the list of students
## self.students.append(student)
## def display_report(self):
## total_credits = 0
## total_study_hours = 0
##
## for student in self.students:
## total_credits += student.credits
## total_study_hours += student.study_hours
## average_credits += total_credits / len(self.students)
## average_study_hours = total_study_hours / len(self.students)
##
## print("Study Hours Report")#display report
## print(f"Created for {self.report_for}, {self.report_employee_id}")
## print(f"Department:{self.report_department}")
## print(f"Created By:{self.creator}, {self.employee_id}")
## print(f"Department: {self.department}")
## print(f"Total Students: {len(self.students)}")
## print(f"Average Credits: {average_credits:.2f}")
## print(f"Average Study Hours: {average_study_hours:.2f}")
def create_report():#get user information
creator_name = input("Enter your name:")
while creator_name == "":
print("Please enter a valid name.")
creator_name = input("Enter your name")
creator_employee_id = input("Enter your employee id:")
while creator_employee_id == "":
print('Please enter a valid employee id.')
creator_employee_id = input("Enter your employee id:")
creator_department = input("Enter your department:")
while creator_department == "":
print(" Please enter a valid department name.")
creator_department = input("Enter your department.")
report1 = Report(creator_name, creator_employee_id, creator_department)#, report_for, report_employee_id, report_department)
report_for = input("Enter the name of the report the person is for:")
report_employee_id = input("Enter the employee id of the person the report is for:")
report_department = input("Enter the department of the person the report is for:")
report2 = Report(report_for, report_employee_id, report_department)
with open("HowManyHours.txt", "r") as file:
lines = file
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
total_study_hours = 15 * credits
elif grade == 'B':
total_study_hours = 12 * credits
elif grade == 'C':
total_study_hours = 9 * credits
elif grade == 'D':
total_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")
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()
def display_averages_and_totals(self):
try:
with open ("HowManyHours.txt", "r") as file:#read data from file
data = file.readlines()
create_report()
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.
print("Study Hours Report\n")
print(f"Created for:{report2.report_for},{report2.report_employee_id}\n")
print(f"Department:{report2.report_department}\n")
print(f"Created By: {report1.creator_name},{report1.creator_employee_id}\n")
print(f"Department:{report1.creator_department}\n")
print(f"Total Students: {total_students}\n")
print(f"Average Credits: {average_credits:.2f}\n")
print(f"Average Study Hours: {average_study_hours:.2f}\n")
## report1 = Report(creator_name, creator_employee_id, creator_department)#, report_for, report_employee_id, report_department)
##
## report_for = input("Enter the name of the report the person is for:")
## report_employee_id = input("Enter the employee id of the person the report is for:")
## report_department = input("Enter the department of the person the report is for:")
##
## report2 = Report(report_for, report_employee_id, report_department)
except FileNotFoundError:
raise FileNotFoundError("HowManyHours.txt is not found.")
if __name__=='__main__':
print('\t\t\tMain Menu')#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.
Grades - Notepad File Edit Format View Help Patrick Mahomes 12 36 DeSHaun WATSON 9 apple drew BREES 55 15 JULIO JONES 15 45 PETER griffin 12 30 Stewey Griffin 15 45 Steven UNIVERSE 9 15 Ringo STARR 18 30 Billy BEAUTY 6 15 Sam Smith 9 30 A Type here to search O jo (99+ Ln 1, Col 1 100% Rain... Windows (CRLF) 0 I 0 UTF-8 9:36 AM 1/25/2023 X 125
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started