Advertisement
brilliant_moves

MultiChoice.py

Nov 28th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. #MultiChoice.py
  2. #Python3
  3.  
  4. '''
  5. python problem involving getting information from a file and formatting the results into columns?
  6. Below is what the input and output should look like in a functioning code.
  7.  
  8. You have been asked to write a program to grade a multiple-choice exam.
  9. The exam has 20 to 30 questions, each answered with a where the first line is the key,
  10. consisting of 20 to 30 characters depending on the number of problems. The remaining lines
  11. on the file are exam answers; each consists of a student ID, a space and a string of characters
  12. (the length of the string matches with the key length). The program should read the name of
  13. the file interactively letter in the range of 'a' through 'f'. The data are stored in a file
  14. and when successful, should read the key followed by student records which are IDs and their answers.
  15. It should then output the ID and student answers followed by their grade and standing.
  16.  
  17. Sample input file abcdefabcdefabcdefab
  18. c1234565 abcdefabcdefabcdefaa
  19. c1234564 bccdefabcdefabcdefcc
  20.  
  21. Sample output:
  22.  
  23. Enter the filename: data6.txt
  24. Key: abcdefabcdefabcdefab 20
  25.  
  26. Student ID  Student answers     Grade   Standing
  27. c1234565    abcdefabcdefabcdefaa    19  above average
  28. c1234564    bccdefabcdefabcdefcc    16  above average
  29.  
  30. The average of the score is 17.5
  31. There are 2 records in the file
  32. The student ID c1234565 have the highest score of 19
  33.  
  34. '''
  35.  
  36. def numCorrect(ans, corr):
  37.     c = 0 # number of correct answers
  38.     for x in range (0, len (corr)):
  39.         if ans[x] == corr[x]:
  40.             c += 1
  41.     return c
  42.  
  43. filename = input ("Enter the filename: ")
  44. first = True
  45. id = []
  46. answers = []
  47. correct = []
  48. total = 0.0
  49. maxID = 0
  50. highest = 0
  51.  
  52. with open (filename, "r") as f:
  53.     for line in f:
  54.         if first:
  55.             key = line.strip()
  56.             first = False
  57.         else:
  58.             li = []
  59.             for i in line.split():
  60.                 li.append(i)
  61.             id.append (li[0])
  62.             answers.append (li[1])
  63.             num_right = numCorrect (li[1], key)
  64.             correct.append (num_right)
  65.             if num_right > highest:
  66.                 maxID = li[0]
  67.                 highest = num_right
  68.  
  69.             total += num_right
  70.  
  71. average = float (total / len (id))
  72.  
  73. print ("Key: %s %d" % (key, len(key)))
  74. print ("\nStudent ID\tStudent answers\t\tGrade\tStanding")
  75. for x in range (len (id)):
  76.     if correct[x] > average:
  77.         grade = "above average"
  78.     elif correct[x] < average:
  79.         grade = "below average"
  80.     else:
  81.         grade = "average"
  82.  
  83.     print ("%s\t%s\t%s\t%s" % (id[x], answers[x], correct[x], grade))
  84.  
  85. print ("\nThe average score is %.1f" % average)
  86. print ("There are %d records in the file." % len(id))
  87. print ("The student ID %s has the highest score of %d" % (maxID, highest))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement