H.Brown
H.Brown

Reputation: 1

How to get an average from a text document in python?

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

Answers (1)

JulienD
JulienD

Reputation: 7293

  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.

Upvotes: 1

Related Questions