Atmtje
Atmtje

Reputation: 3

How can I change my output so the grades are behind the names?

first of all I'm a complete programming noob but I had to do this small assignment for school to pass so it would really help me out if someone could give me the last answer to my question. (BTW I'M USING THE LATEST PYTHON)

So I will summarise the assignment: I received an .txt file with a list of 10 students, after every students name there are 3 grades (the lowest grade can be a 1 and the highest grade a 10).

Small example of how the list looks:

Tom Bombadil__________6.5 5.5 4.5

Dain IJzervoet________6.7 7.2 7.7

Thorin Eikenschild____6.8 7.8 7.3

Now I need to type a code that will exactly give this output when I run the program:

Report for group 2b

Tom Bombadil has an average grade of 5.5

Dain IJzervoet has an average grade of 7.2

Thorin Eikenschild has an average grade of 7.3

Meriadoc Brandebok has an average grade of 4.6

Sam Gewissies has an average grade of 4.5

Gollem has an average grade of 1.8

Frodo Ballings has an average grade of 6.8

Gandalf de Grijze has an average grade of 9.5

Peregrijn Toek has an average grade of 6.2

Radagast de Bruine has an average grade of 8.8

End of report

This is my code now:

NUMBER_OF_GRADES = 3

def print_geo_grades(input_grades):
    all_grades = list(map(float, input_grades.split(" ")))

    first_grade = all_grades[0]
    second_grade = all_grades[1]
    third_grade = all_grades[2]

    average_grade = (first_grade + second_grade + third_grade) / NUMBER_OF_GRADES

    print("%.1f" % average_grade)

def print_student(all_students):
    student_details = all_students.split("_")

    full_name_student = student_details[0]
    all_grades = student_details[-1]

    print("%s has an average grade of " % full_name_student, print_geo_grades(all_grades))

'''Start Program'''
print("Report for group 2b")
students = open('geo_grades1.in.txt').readlines()

for each_student in students:
    print_student(each_student)

print("End of report")

And this is the output it now gives me:

Report for group 2b
5.5
Tom Bombadil has an average grade of  None
7.2
Dain IJzervoet has an average grade of  None
7.3
Thorin Eikenschild has an average grade of  None
4.6
Meriadoc Brandebok has an average grade of  None
4.5
Sam Gewissies has an average grade of  None
1.8
Gollem has an average grade of  None
6.8
Frodo Ballings has an average grade of  None
9.5
Gandalf de Grijze has an average grade of  None
6.2
Peregrijn Toek has an average grade of  None
8.8
Radagast de Bruine has an average grade of  None
End of report

Process finished with exit code 0

As you can see are the average grades in front of the names and where the averages should be it says 'None'. Please guys I'm almost there and I need to finish this before friday 23:59. PLEASE HELP A BROTHER OUT!

Upvotes: 0

Views: 101

Answers (4)

Nikhil Ghule
Nikhil Ghule

Reputation: 124

The function you are calling doesn't return any value. you can fix this in two ways

  1. instead of printing just return average_grade

  2. call print_geo_grades() outside the print statement

    use any one.

The string formating you are using in print statement is really old fashioned, instead refer the code below

    NUMBER_OF_GRADES = 3

def print_geo_grades(input_grades):
    all_grades = list(map(float, input_grades.split(" ")))

    first_grade = all_grades[0]
    second_grade = all_grades[1]
    third_grade = all_grades[2]

    average_grade = (first_grade + second_grade + third_grade) / NUMBER_OF_GRADES
#just return python will not consider the zeroes after decimal
    return average_grade                                                                                        

def print_student(all_students):
    student_details = all_students.split("_")

    full_name_student = student_details[0]
    all_grades = student_details[-1]
 #new way of writing
    print("{} has an average grade of {}".format(full_name_student, print_geo_grades(all_grades)))             

'''Start Program'''
print("Report for group 2b")
students = open('geo_grades1.in.txt').readlines()
for each_student in students:
    print_student(each_student)

print("End of report")



>Report for group 2b
>Tom Bombadil has an average grade of 5.5
>Dain IJzervoet has an average grade of 7.2
>Thorin Eikenschild has an average grade of 7.3
>End of report

Upvotes: 0

DavideBrex
DavideBrex

Reputation: 2424

You need to return the grade from the function print_geo_grades instead of printing it. Just add return and remove print from the function and it should work:

def print_geo_grades(input_grades):
    all_grades = list(map(float, input_grades.split(" ")))

    first_grade = all_grades[0]
    second_grade = all_grades[1]
    third_grade = all_grades[2]

    average_grade = (first_grade + second_grade + third_grade) / NUMBER_OF_GRADES

    return round(average_grade,1)

Upvotes: 1

user4756260
user4756260

Reputation: 111

Basically, the simplest solution is:

import re
from statistics import mean


with open("marks.txt") as fd:
    for line in fd:
        name, raw_marks = re.split(r"_+", line)
        marks = map(float, raw_marks.split())
        print(f"{name} has average grade {mean(marks)}")

Upvotes: 0

Prathamesh
Prathamesh

Reputation: 1057

You forgot to return your average_grade, just add a,

return average_grade statement to your function such as,

def print_geo_grades(input_grades):
    all_grades = list(map(float, input_grades.split(" ")))

    first_grade = all_grades[0]
    second_grade = all_grades[1]
    third_grade = all_grades[2]

    average_grade = (first_grade + second_grade + third_grade) / NUMBER_OF_GRADES

    return average_grade

Hope this helps you!

Upvotes: 0

Related Questions