1.

Solve : Any Python Experts Here????

Answer»

Currently doing GCSE computing where we have coursework which has to meet the deadline however HELP is required from experts that have greater knowledge using this.

Task 1 has been completed (designing mathematical quiz for students generating random multiplication questions) Task 2 has been completed (creating .TXT file for storing the data students got on the mathematical TEST) however Task 3 requires you to sort the data into alphabetical order, highest to LOWEST score and average score which has not been completed because help is needed.

The code which I have currently is...

results = input("Which class would you like to see: A, B or C? \n")
if results =="A":
print ("Mathematical test data for class A has been sorted in alphabetically order")
with open("ScoresClassA.txt", 'r') as r:
for LINE in sorted(r):
print(line, "\n", end='')

if results =="B":
print ("Mathematical test for class B has been sorted in alphabetically order")
with open("ScoresClassB.txt", 'r') as r:
for line in sorted(r):
print(line, "\n", end='')

But when I run the code it sorts the .txt in alphabetical order when I input one class but it gives below the alphabetical order for class B which I do not want because I requested for one class to be shown when it asked me to input the name ?

How would I do the highest score?
How would I do it by the average?

Thank you!
Well since you did not surround your code with code tags it is hard to tell whether or not you have your code indented properly. Too me it does not look like you have the code indented properly so the interpreter will always execute the second with statement and print the sorted B file.

But in my opinion, if you have three options to check you should use IF ELIF statements.when you open the data, add it to any array (as are, but then sort it manually (not with the sorted() function). How are you storing the data in the text file? You may need to do something like this:
Code: [Select]data = open("filename",'r')
data = data.readlines()
for line in data:
line = line.replace("\n","").split("delimiter")

# Sort algorithm here

Look up what kind of sorting algorithm is most efficient for your specific case and implement it there. Then you have a sorted list and you do so your print loop.



Discussion

No Comment Found