Home > front end >  Computing Averages from columns and rows that have text and integers
Computing Averages from columns and rows that have text and integers

Time:04-05

I've never been more lost coding, I'll link the specifics but i have no clue how to take averages from specific columns or rows from a file. So far I've been able to: Open the file, Split the columns, and assign grade values. I'm struggling to : Average out specific values from the columns/rows, and to write that on a new file. I know what i need to do but I don't know how. any help would be super appreciated SPECIFIC DETAILS

file = input()      


with open(file) as f: 
    data = f.read()
    data = data.split("\t")
    for line in f:
        student_average = line.mean()
        if student_average >= 90:
            grade = "A"
        elif 80 <= student_average < 90:
            grade = "B"
        elif 70 <= student_average < 80:
            grade = "C"
        elif 60 <= student_average < 70:
            grade = "D"
        else:
            Grade = "F"
    
        

CodePudding user response:

Three issues: You are using a none existent mean() method. You are trying to iterate through a file object. And your numbers are string, they need to be converted to number datatype.

import statistics
data = '90.5\t80.1\t70.9'  # <--- assuming this is the data structure of your file
data = data.split("\t")
data = [float(d) for d in data]
avg = statistics.mean(data)
print('average:', avg)

CodePudding user response:

if student_average >= 90:
    grade = "A"  
elif 80 <= student_average < 90:
    grade = "B"

makes no sense ... you probably ment to do elif student_average >= 80: as you are already sure its < 90 else wise if student_average >= 90: would have matched.

Also bad:

  • no conversion from string to number
  • no mean calculation
  • exhausting the file by data = f.read() and then trying to do for line in f: etc.

Fix:

with open ("t.txt","w" ) as f:
    f.write("95\t95\t95\t95\t95\t95\n")   
    f.write("85\t85\t85\t85\t85\t85\n")   
    f.write("75\t75\t75\t75\t55\t65\n")   
    f.write("65\t65\t65\t65\t65\t65\n")   
    f.write("25\t35\t45\t55\t65\t75\n")   

def mean(itrable):
    return sum(itrable)/len(itrable)


with open("t.txt") as f: 
    for line in f:
        line = list(map(int,line.strip().split("\t")))
        student_average = mean(line)
        if student_average >= 90:
            grade = "A"
        elif student_average >= 80:  # only reached if not >= 90
            grade = "B"
        elif student_average >= 70:
            grade = "C"
        elif student_average >= 60:
            grade = "D"
        else:
            grade = "F"

        print(line, student_average, grade, sep=" ==> ")

Output:

[95, 95, 95, 95, 95, 95] ==> 95.0 ==> A
[85, 85, 85, 85, 85, 85] ==> 85.0 ==> B
[75, 75, 75, 75, 55, 65] ==> 70.0 ==> C
[65, 65, 65, 65, 65, 65] ==> 65.0 ==> D
[25, 35, 45, 55, 65, 75] ==> 50.0 ==> F

CodePudding user response:

I wont do the whole thing, but you can split by lines first and split by tabs again after. You can add your own logic after...

#/usr/bin/python3

StudentsInfoFile = open("StudentsInfo.tsv")

data =  StudentsInfoFile.read().split("\n")

for item in data:
    if item:
        row = item.split("\t")
        print(row)
        first_name = row[0]
        last_name = row[1]
        exam1 = row[2]
        exam2 = row[3]
        print("first name : "   first_name)
        print("last name : "   last_name)
        print("first grade : "   exam1)
  • Related