Advertisement
Guest User

329Y4IWO8DMFNYU3W5YTUU

a guest
Apr 27th, 2015
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.28 KB | None | 0 0
  1. # import pickle
  2. import pickle
  3.  
  4. def view_results(classfile, classroom):
  5.     # view scores (alphabetically, by highest score or by average score)
  6.     while 1:
  7.         print("Show %s" %(classroom))
  8.         print("[1] In Alphabetical Order")
  9.         print("[2] By Highest Score")
  10.         print("[3] By Average Score")
  11.         print("[4] Back to Rooms")
  12.         choice = input("> ")
  13.  
  14.         if choice is '1':
  15.             classfile.sort(key=lambda s: s[0], reverse=False)
  16.             # sort alphabetically
  17.             print("Sorted In Alphabetical Order")
  18.             for i in classfile:
  19.                 score = sorted(i[1], reverse=True)
  20.                 # display the scores with the square brackets removed
  21.                 print(i[0].title() + '\t' + str(score).replace('[','').replace(']',''))
  22.             input()
  23.  
  24.             # the problem is here ------------------------------------
  25.         elif choice is '2':
  26.             classfile.sort(key=lambda s: s[1], reverse=False)
  27.             # by highest score
  28.             print("Sorted By Highest Score")
  29.             for i in classfile:
  30.                 score = sorted(i[1], reverse=True)
  31.                 # display the scores with the square brackets removed
  32.                 print(i[0].title() + '\t' + str(score).replace('[','').replace(']',''))
  33.             input()
  34.             # the problem is here -------------------------------------
  35.  
  36.         elif choice is '3':
  37.             # by average score
  38.             classfile.sort(key=lambda s: s[1], reverse=True)
  39.             for i in classfile:
  40.                 # get the average and round it
  41.                 score = round(sum(i[1]) / 3)
  42.                 print(i[0].title() + '\t' + str(score))
  43.             input()
  44.  
  45.         elif choice is '4':
  46.             # go back to selection
  47.             break
  48.  
  49.         else:
  50.             pass
  51.  
  52. # check for CLASS1.DAT
  53. try:
  54.     classfile1 = pickle.load(open("class1.dat", 'rb'))
  55. except IOError:
  56.     isClassroom1 = False
  57. else:
  58.     isClassroom1 = True
  59.  
  60. # check for CLASS2.DAT
  61. try:
  62.     classfile2 = pickle.load(open("class2.dat", 'rb'))
  63. except IOError:
  64.     isClassroom2 = False
  65. else:
  66.     isClassroom2 = True
  67.  
  68. # check for CLASS3.DAT
  69. try:
  70.     classfile3 = pickle.load(open("class3.dat", 'rb'))
  71. except IOError:
  72.     isClassroom3 = False
  73. else:
  74.     isClassroom3 = True
  75.  
  76. while 1:
  77.     print("Primary School Arithmetic Quiz Viewer")
  78.     print("Please choose a classroom: ")
  79.     if bool(isClassroom1):
  80.         print("[1] Classroom 1")
  81.     else:
  82.         print("file not found: CLASS1.DAT")
  83.     if bool(isClassroom2):
  84.         print("[2] Classroom 2")
  85.     else:
  86.         print("file not found: CLASS2.DAT")
  87.     if bool(isClassroom3):
  88.         print("[3] Classroom 3")
  89.     else:
  90.         print("file not found: CLASS3.DAT")
  91.     print("[4] Exit")
  92.     classfile = input("> ")
  93.    
  94.     if classfile is '1':
  95.         if bool(isClassroom1):
  96.             view_results(classfile1, "Classroom 1")
  97.         else:
  98.             pass
  99.  
  100.     elif classfile is '2':
  101.         if bool(isClassroom2):
  102.             view_results(classfile2, "Classroom 2")
  103.         else:
  104.             pass
  105.  
  106.     elif classfile is '3':
  107.         if bool(isClassroom3):
  108.             view_results(classfile3, "Classroom 3")
  109.         else:
  110.             pass
  111.  
  112.     elif classfile is '4':
  113.         exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement