0

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)
8
  • What is not working? Commented Mar 30, 2016 at 14:36
  • Making an average score from the text document Commented Mar 30, 2016 at 14:44
  • Where are you facing the problem exactly and why did you define menu2==5 in a seperate function? Commented Mar 30, 2016 at 14:50
  • Well i'm not sure how to actually work out an average i was just trying different ideas. I have set menu2==5 because it prints a menu for the user to select how they want to sort their data. Commented Mar 30, 2016 at 14:52
  • Please provide code that runs. This one will not. 1. menu2 is not defined in average(). 2. average is the same name for a function and a variable inside of it. 3. The code is not indented correctly. 4. The average == inside the sorted(, etc. Commented Mar 30, 2016 at 15:28

1 Answer 1

1
  1. Write a function that takes a text file and returns the numbers in a list. It may also take a parameter menu2, that maybe the user will give at some point, but provide a default value for now.
  2. Write a function that takes a list of numbers and computes the average that you want from them.
  3. Write tests with fixed input to make sure that the two functions above work, independently. Give for instance a list of '1's to function 2. and see it it returns the expected average.
  4. Write a function that listens to the user input, and calls function 1. accordingly. Call function 2. on the result.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.