Home > Software design >  Finding average of the text file with function call [closed]
Finding average of the text file with function call [closed]

Time:09-21

Cannot be showing data on output used text file on their numbers

$ ./name.py

File name: qz_04_grades.txt

Average 80

#! /usr/bin/python3
def open_file_read(filename):
        try:
                file = open(filename, 'r')
        except:
                print('None')
def average_score(file):
        total = 0
        count = 0
        for line in filename:
                score, first, last = line.split()
                count  = 1
                total  = int(score)
        file.close()
        return round(total/count)
filename = input("File name: ")
file = open_file_read(filename)
if file:
    average = average_score(file)
    print()
    print("Average", average)

CodePudding user response:

Welcome @Colin Kazim!

I'm not exactly sure what your question is but I gave it my best shot!

I think the code I put here does what you are trying to do! please let me know if I misunderstood or if you have question!

def open_file_read(filename):
        try:
            with open(filename,'r') as file:
                lines = file.readlines()
                return lines
        except:
            print('None')
def average_score(filename):
        lines = open_file_read(filename)
        average = []
        for i in lines:
            score, first, last = i.split()
            average.append(int(score))
        return(sum(average)/len(average))

filename = input("File name: ")
print("Average ", average_score(filename))

CodePudding user response:

Try replacing the last line with:

print(f"Average {average}")
  • Related