python - Calling a function to execute a data tree -


i have code 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 call weighted_score in new function overall_grade. how call weighted_score gives me correct answer? when code executed, example, 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 a")      elif (90<=weighted_total_score<93):         print("the overall student grade a-")      elif (87<=weighted_total_score<90):         print("the overall student grade b+")       elif (83<=weighted_total_score<87):         print("the overall student grade b")       elif (80<=weighted_total_score<83):         print("the overall student grade b-")      elif (77<=weighted_total_score<80):         print("the overall student grade c+")      elif (73<=weighted_total_score<77):         print("the overall student grade c")      elif (70<=weighted_total_score<73):         print("the overall student grade c-")       elif (67<=weighted_total_score<70):         print("the overall student grade d+")      elif (63<=weighted_total_score<67):        print("the overall student grade d")      elif (60<=weighted_total_score<63):        print("the overall student grade d-")      elif (weighted_total_score<60):       print("the overall student grade f") 

how call weighted_score?

you call other method...

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

note don't name variables or parameters weighted_total_score because have method name already. if referenced local variables, shadow method, not , causes confusion beginners.


the reason f because weighted_total_score=int() same weighted_total_score=0, , if statements go way bottom.


also, tip, don't need both boundaries in conditions because condition can "fall through".

and suggestion, try write simple methods, build on top of them. don't @ once. example, make method returns letter grade, have method prints string , uses result of other method.

def get_letter_grade(score):     if (93 <= score):         return "a"     elif (90 <= score): # < 93         return "a-"     elif (87 <= score): #  < 90         return "b+"     # ... etc     else:               # < 60         return "f"  def overall_grade(scores):     weighted_score = weighted_total_score(scores)     print("the overall grade {}".format(get_letter_grade(weighted_score))) 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -