Advertisement
Guest User

Student Grades

a guest
Jun 19th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. admins = {'Pete':'123','John':'321'}
  2. usernameInput = ''
  3. passInput = ''
  4. gradesDict = {'Pete':[1,2,3],'John':[9,8,7],'Johny':[]}
  5.  
  6. from statistics import *
  7.  
  8. #Check if the student name exists as key in the dictionary
  9. def studentExists(name):
  10.     flag = False
  11.  
  12.     if name in gradesDict:
  13.         flag = True
  14.  
  15.     return flag
  16.  
  17.  
  18. #Adding the student key and grade to the dictionary
  19. def addStudent(name, grade):
  20.  
  21.     gradesDict[name] = [grade]
  22.     print('\nUpdated list: ',gradesDict)
  23.  
  24.  
  25. #Adding the already existing student grade to the dictionary
  26. def addGrade(name, grade):
  27.  
  28.     gradesDict[name].append(grade)
  29.     print('\nUpdated list: ',gradesDict)
  30.  
  31. #Passing the name of the student we want to remove from the main script
  32. def removeStudent(name):
  33.    
  34.     del gradesDict[name]
  35.     print('\nRemoving student: ',name)
  36.     print('\nUpdated list: ',gradesDict)
  37.  
  38. def getAverage(name):
  39.     print('\n'+name,'average grade is:',mean(gradesDict[name]))
  40.    
  41. def getAverageAll():
  42.     for student in gradesDict:
  43.         try:
  44.             gradeList = gradesDict[student]
  45.             print(student,'average grade is:',mean(gradesDict[student]))
  46.         except StatisticsError as se:
  47.             print(student,'average grades cannot be calculated!')
  48.  
  49. #MAIN PROGRAM
  50. while usernameInput not in admins or admins[usernameInput]!=passInput:
  51.     usernameInput = input('Username:')
  52.     passInput = input('Password:')
  53.  
  54.     if usernameInput not in admins or admins[usernameInput]!=passInput:
  55.         print('\nPlease provide valid username and password!\n')
  56.  
  57. print('\nWelcome to the Grading Centre')
  58.  
  59. while True:
  60.  
  61.     try:
  62.         menuInput = input("""
  63. [1] - Enter Grades
  64. [2] - Remove Student
  65. [3] - Calculate Average Grade of student
  66. [4] - Get Average Grade for each student
  67. [5] - Exit
  68.  
  69. What would you like to do today? [Enter a number from the menu]: """)
  70.         menuInput = int(menuInput)
  71.     except Exception as e:
  72.         print(e)
  73.  
  74.        
  75.     if menuInput == 1:
  76.         name = ''
  77.         grade = -1
  78.  
  79.         name = input('\nStudent Name: ')
  80.        
  81.        
  82.         while grade <0 or grade >100:
  83.             try:
  84.                 grade = input('Student Grade: ')
  85.                 grade = float(grade)
  86.                
  87.                 if(grade<0 or grade >100):
  88.                     print('\nEnter grade between 0 and 100!\n')
  89.                    
  90.             except Exception as e:
  91.                 print('\nPlease enter valid grade!\n')
  92.                 grade = -1
  93.  
  94.         if studentExists(name):
  95.             addGrade(name, grade)
  96.         else:
  97.             add = input('\nThe student you have entered does not exist, please confirm if you want to add a new Student? [Y/N]\n')
  98.             add = add.upper()
  99.  
  100.             if add == 'Y':
  101.                 addStudent(name, grade)
  102.  
  103.                
  104.     elif menuInput == 2:
  105.  
  106.         print('Student list: ',gradesDict,'\n')
  107.         print('Please enter a student name from the list or type \'q\' to go back to the menu!')
  108.        
  109.         name = ''
  110.         count = 0
  111.        
  112.         while not(studentExists(name)) and name.lower() != 'q':
  113.            
  114.             if count > 0:
  115.                 print('\nPlease enter valid student name!')
  116.                
  117.             name = input('\nStudent Name: ')
  118.            
  119.             count+=1
  120.  
  121.         if studentExists(name):
  122.             removeStudent(name)
  123.  
  124.        
  125.     elif menuInput == 3:
  126.         print('Student list: ',gradesDict,'\n')
  127.         print('Please enter a student name from the list or type \'q\' to go back to the menu!')
  128.        
  129.         name = ''
  130.         count = 0
  131.        
  132.         while not(studentExists(name)) and name.lower() != 'q':
  133.                        
  134.             name = input('\nStudent Name: ')
  135.  
  136.             if not(studentExists(name)):
  137.                 print('\nPlease enter valid student name or press \'q\'!')
  138.            
  139.             if studentExists(name):
  140.                 try:
  141.                     getAverage(name)
  142.                 except StatisticsError as e:
  143.                     name = ''
  144.                     print('\nThe student you have entered has no grades!')
  145.     elif menuInput == 4:
  146.         getAverageAll()
  147.            
  148.     elif menuInput == 5:
  149.         exit()
  150.  
  151.     else:
  152.        
  153.         print('\nPlease enter a valid option from the menu!\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement