Question: class StudentInfoError ( Exception ) : Custom exception for student information errors. def _ _ init _ _ ( self , message

class StudentInfoError(Exception):
"""Custom exception for student information errors."""
def __init__(self, message):
self.message = message
super().__init__(self.message)
def find_ID(name, student_dict):
"""Find the student ID by name."""
if name in student_dict:
return student_dict[name]
else:
raise StudentInfoError(f"Student ID not found for {name}")
def find_name(student_id, student_dict):
"""Find the student name by ID."""
for name, id in student_dict.items():
if id == student_id:
return name
raise StudentInfoError(f"Student name not found for {student_id}")
def main():
student_dict ={
'Reagan': 'rebradshaw835',
'Ryley': 'rbarber894',
'Peyton': 'pstott885',
'Tyrese': 'tmayo945',
'Caius': 'ccharlton329'
}
try:
user_choice = int(input("Enter 0 to find ID by name or 1 to find name by ID: "))
user_input = input("Enter the student name or ID: ")
if user_choice ==0:
# Find ID by name
result = find_ID(user_input, student_dict)
print(result)
elif user_choice ==1:
# Find name by ID
result = find_name(user_input, student_dict)
print(result)
else:
print("Invalid choice. Please enter 0 or 1.")
except StudentInfoError as e:
print(e.message)
except ValueError:
print("Invalid input. Please enter a valid integer for user choice.")
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!