Reputation: 1
In python. I need to calculate an average from a text document. Then line that is set out in the text document looks like this Bob Farnworth 11SM 1 out of 10 I have managed to sort it alphabetically and by score. Each time someone runs the test and completes it, it will store their data in the text document on a new line but i need to sort their score by average by adding each persons score up and dividing it by how many there are. This is what i have got so far.
def sortdata(): #This is my menu that will allow the user to enter a number to sort the data thats been collected from the test
while True:
print("")
print("Sort menu")
print("Enter 1 to sort by Surname")
print("Enter 2 to sort by Firstname")
print("Enter 3 to sort by Form")
print("Enter 4 to sort by Score")
print("Enter 5 to sort by Average")
menu2 = input("Enter Number..")
print("")
try:
menu2 = int(menu2)
except ValueError:
print ("unacceptable")
break
if menu2 == 1:
with open('data.txt', 'r') as data:
for line in sorted(data.readlines(), key=lambda line:(line.split(' ')[1])):
print(line, end='')
if menu2 == 2:
with open('data.txt', 'r') as data:
for line in sorted(data.readlines(), key=lambda line:(line.split(' ')[0])):
print(line, end='')
if menu2 == 3:
with open('data.txt', 'r') as data:
for line in sorted(data.readlines(), key=lambda line:(line.split(' ')[2])):
print(line, end='')
if menu2 == 4:
with open('data.txt', 'r') as data:
for line in sorted(data.readlines(), key=lambda line:(line.split(' ')[3])):
print(line, end='')
def average():
if menu2 ==5:
with open('data.txt', 'r') as data:
sum = 10
for line in sorted(data.readlines(), average == sum/(line.split(' ')[3])):
print (average)
Upvotes: 0
Views: 73
Reputation: 7293
menu2
, that maybe the user will give at some point, but provide a default value for now.Some would say that you should even write the tests first, and code until the tests pass.
Also, when you ask a question about code that does not work, always provide the error message you got.
Upvotes: 1