For Python 3:
Write a program that keeps a dictionary in which both keys and values are strings – names of students and their course grades. Your program should prompt the user with a menu consisting of the following options: Select an option: [1] Add Student Grade [2] Remove Student Grade [3] Modify Student Grade [4] Display All Student Grades Enter selection: If the user enters option 1, as shown below, the program shall behave as shown: Enter selection: 1 –Add Student– Enter Name: Joe Enter Grade: B+ Student Successfully Added! If the user enters option 2, the program shall behave as follows: Enter selection: 2 –Remove Student– Enter Name: Joe Successfully Removed ‘Joe’! If the user enters option 3, the program shall behave as follows Enter selection: 3 -Modify Student Grade– Enter Name: Joe Enter Grade C Successfully Updated ‘Joe’! The display produced by option 4 should show each student’s name with their corresponding grade with the following formatting: Enter selection: 4 –Displaying All Students– Carl: B+ Joe: C Sarah: A Francine: A
Expert Answer
Python Program:
def menu():
“”” Displays menu to user “””
# Printing menu
print(“nn Select an option: n [1] Add Student Grade n [2] Remove Student Grade n [3] Modify Student Grade n [4] Display All Student Grades n [5] Exit n”);
# Accepting user input
ch = int(input(“n Enter selection: “));
return ch;
def addGrade(studentGrades):
“”” Function that adds student grades “””
print(“nn –Add Student– “);
# Accepting user input
name = input(“n Enter Name: “);
grade = input(” Enter Grade: “);
# Adding to dictionary
studentGrades[name] = grade;
print(“n Student Successfully Added! n”);
def removeGrade(studentGrades):
“”” Function that removes student grade “””
print(“nn –Remove Student– “);
# Accepting user input
name = input(“n Enter Name: “);
# Checking for existence
if name in studentGrades.keys():
# Deleting student
del studentGrades[name];
print(“n Successfully Removed ‘%s’ n” %(name));
else:
print(“n Student ‘%s’ not found!!! n” %(name));
def modifyGrade(studentGrades):
“”” Function that updates student grade “””
print(“nn –Modify Student Grade– “);
# Accepting user input
name = input(“n Enter Name: “);
grade = input(” Enter Grade: “);
# Checking for existence
if name in studentGrades.keys():
# Deleting student
studentGrades[name] = grade;
print(“n Successfully Updated ‘%s’ n” %(name));
else:
print(“n Student ‘%s’ not found!!! n” %(name));
def displayGrades(studentGrades):
“”” Function that displays student grades “””
print(“nn –Displaying All Students– “);
# Iterating over dictionary
for name in studentGrades.keys():
# Printing student
print(“n %s: %s” %(name, studentGrades[name]));
def main():
“”” Main function “””
# Dictionary to hold items
studentGrades = {};
# Iterate till user want to quit
while True:
# Displaying menu
ch = menu();
# Calling appropriate functions
if ch == 1:
addGrade(studentGrades);
elif ch == 2:
removeGrade(studentGrades);
elif ch == 3:
modifyGrade(studentGrades);
elif ch == 4:
displayGrades(studentGrades);
elif ch == 5:
return;
else:
print(“n Invalid choice…. n”);
# Calling main function
main();
________________________________________________________________________________________________
Code Screen Shots:
Sample Run: