Search code examples
pythondata-structuresaverageweighted-average

Calling a function to execute a data tree


I already have a code for weighted score.

def weighted_total_score(student_scores):
    return((int(student_scores[0])*mid_1_weight)+(int(student_scores[1])*mid_2_weight)+(int(student_scores[2])*final_exam_weight)+(int(student_scores[3])*homework_weight)+(int(student_scores[4][0])*lab_weight)+(int(student_scores[5])*pr_1_weight)+(int(student_scores[6])*pr_2_weight)+(int(student_scores[7])*pr_3_weight)+(int(student_scores[8])*participation_weight))

I would like to call weighted_score in my new function overall_grade. How do i call weighted_score so that it gives me the correct answer? Currently when my code is executed, for example, I am getting F instead of C.

def overall_grade(weighted_total_score):
    weighted_total_score=int()
    if (weighted_total_score >=93):
        print("The overall student grade is A")

    elif (90<=weighted_total_score<93):
        print("The overall student grade is A-")

    elif (87<=weighted_total_score<90):
        print("The overall student grade is B+")


    elif (83<=weighted_total_score<87):
        print("The overall student grade is B")


    elif (80<=weighted_total_score<83):
        print("The overall student grade is B-")


   elif (77<=weighted_total_score<80):
        print("The overall student grade is C+")


   elif (73<=weighted_total_score<77):
        print("The overall student grade is C")


   elif (70<=weighted_total_score<73):
        print("The overall student grade is C-")



   elif (67<=weighted_total_score<70):
        print("The overall student grade is D+")



  elif (63<=weighted_total_score<67):
       print("The overall student grade is D")



  elif (60<=weighted_total_score<63):
       print("The overall student grade is D-")



  elif (weighted_total_score<60):
      print("The overall student grade is F")

Solution

  • How do i call weighted_score?

    You call it like any other method...

    def overall_grade(scores):
        score = weighted_total_score(scores)
    

    Note Don't name your variables or parameters weighted_total_score because you have a method with that name already. If you referenced your local variables, they would shadow that method, which is generally not good and causes confusion for beginners.


    The reason you get F is because weighted_total_score=int() is the same as weighted_total_score=0, and your if statements go all the way to the bottom.


    Also, tip, you actually don't need both boundaries in your conditions because the condition can "fall through".

    And a suggestion, try to write simple methods, then build on top of them. Don't do too much at once. For example, make a method that only returns the letter grade, then have the method that prints the string and uses the result of the other method.

    def get_letter_grade(score):
        if (93 <= score):
            return "A"
        elif (90 <= score): # already < 93
            return "A-"
        elif (87 <= score): # already  < 90
            return "B+"
        # ... etc
        else:               # < 60
            return "F"
    
    def overall_grade(scores):
        weighted_score = weighted_total_score(scores)
        print("The overall grade is {}".format(get_letter_grade(weighted_score)))