Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #MultiChoice.py
- #Python3
- '''
- python problem involving getting information from a file and formatting the results into columns?
- Below is what the input and output should look like in a functioning code.
- You have been asked to write a program to grade a multiple-choice exam.
- The exam has 20 to 30 questions, each answered with a where the first line is the key,
- consisting of 20 to 30 characters depending on the number of problems. The remaining lines
- on the file are exam answers; each consists of a student ID, a space and a string of characters
- (the length of the string matches with the key length). The program should read the name of
- the file interactively letter in the range of 'a' through 'f'. The data are stored in a file
- and when successful, should read the key followed by student records which are IDs and their answers.
- It should then output the ID and student answers followed by their grade and standing.
- Sample input file abcdefabcdefabcdefab
- c1234565 abcdefabcdefabcdefaa
- c1234564 bccdefabcdefabcdefcc
- Sample output:
- Enter the filename: data6.txt
- Key: abcdefabcdefabcdefab 20
- Student ID Student answers Grade Standing
- c1234565 abcdefabcdefabcdefaa 19 above average
- c1234564 bccdefabcdefabcdefcc 16 above average
- The average of the score is 17.5
- There are 2 records in the file
- The student ID c1234565 have the highest score of 19
- '''
- def numCorrect(ans, corr):
- c = 0 # number of correct answers
- for x in range (0, len (corr)):
- if ans[x] == corr[x]:
- c += 1
- return c
- filename = input ("Enter the filename: ")
- first = True
- id = []
- answers = []
- correct = []
- total = 0.0
- maxID = 0
- highest = 0
- with open (filename, "r") as f:
- for line in f:
- if first:
- key = line.strip()
- first = False
- else:
- li = []
- for i in line.split():
- li.append(i)
- id.append (li[0])
- answers.append (li[1])
- num_right = numCorrect (li[1], key)
- correct.append (num_right)
- if num_right > highest:
- maxID = li[0]
- highest = num_right
- total += num_right
- average = float (total / len (id))
- print ("Key: %s %d" % (key, len(key)))
- print ("\nStudent ID\tStudent answers\t\tGrade\tStanding")
- for x in range (len (id)):
- if correct[x] > average:
- grade = "above average"
- elif correct[x] < average:
- grade = "below average"
- else:
- grade = "average"
- print ("%s\t%s\t%s\t%s" % (id[x], answers[x], correct[x], grade))
- print ("\nThe average score is %.1f" % average)
- print ("There are %d records in the file." % len(id))
- print ("The student ID %s has the highest score of %d" % (maxID, highest))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement