Over the past few days, I have been attempting to code a solution to Task 3 of the A453 Computing section. Essentially, I am aiming to produce a program that takes data from members of a class that have participated in a maths quiz (that is situated over three text files, as the exam specification states that participants in the hypothetical quiz must be from a "Class 1" "Class 2", or "Class 3")and then sorts it, according to the user's input, in one of the two following ways:

• In alphabetical order with each student's (in the appropriate class) highest score for the tests.

• By the highest score achieved by each student in that class, from highest to lowest.

Here is the code that I have written up so far:

``def highest_score_first():names_with_scores_list = []ScoresFirstList = []AlreadySeenList = []for line in file:names_with_scores_list.append(line.strip())for row in names_with_scores_list:nameString = row[0: row.index(', ') ]scoreString = row[row.index(', ')+2 : len(row)]scoresFirstString = scoreString+","+nameString#print(scoresFirstString)ScoresFirstList.append(scoresFirstString)ScoresFirstList.sort(reverse = True)for row in ScoresFirstList:nameString = row[row.index(',') : len(row)]if nameString not in AlreadySeenList:print(row)AlreadySeenList.append(nameString)def alphabetical():names_with_scores_list = []NamesFirstList = []AlreadySeenList = []for line in file:names_with_scores_list.append(line.strip())for row in names_with_scores_list:nameString = row[0: row.index(', ') ]scoreString = row[row.index(', ')+2 : len(row)]NamesFirstString = nameString+","+scoreStringNamesFirstList.append(NamesFirstString)NamesFirstList.sort()for row in NamesFirstList:nameString = row[0: row.index(',') ]if nameString not in AlreadySeenList:print (row)AlreadySeenList.append(nameString)# main code herechosen_class = input("Which class would you like to view - one, two or three?")if chosen_class == "one":file = open("classonescore.txt","r")elif chosen_class == "two":file = open("classtwoscore.txt","r")elif chosen_class == "three":file = open("classthreescore.txt","r")else:print("Unfortunately, you have entered an invalid class name. ")function = input("How would you like to sort the data - alphabetically or by highest score? Choose A or H")if function.upper() == "H":highest_score_first()elif function.upper() == "A":alphabetical()``

Essentially, the problem with the code is that, when the user wants the data for a class to be sorted alphabetically (in terms of highest score), only the lowest score for each student, as well as his name, is generated.

For instance, whilst data from Class 1 is written into the text file as follows:

Chris, 08

Clive, 09

Will, 04

Harry, 10

Ahmed, 08

Geoff, 06

Amy, 04

Vennu, 10

Vennu, 07

Amy, 06

Ahmed, 09

Geoff, 04

Harry, 07

Will, 06

Clive, 10

Chris, 10

It is displayed upon running the program (when the user wants scores to be generated using "alphabetical") as:

Ahmed,08

Amy,04

Chris,08

Clive,09

Geoff,04

Harry,07

Vennu,07

Will,04

Ideally, it should be generated as follows:

Ahmed, 09

Amy, 06

Chris, 10

Clive, 10

Geoff, 06

Harry, 10

Vennu, 10

Will, 06

Unfortunately, I am unable to contact my teacher, so any reply would be greatly appreciated.

Many thanks,

Chris

Top