调用函数来执行数据树

时间:2016-05-25 02:45:51

标签: python data-structures average weighted-average

我已经有加权分数的代码。

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))

我想在我的新函数overall_grade中调用weighted_score。我如何调用weighted_score以便它给出正确的答案?例如,当我执行代码时,我得到的是F而不是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")

2 个答案:

答案 0 :(得分:1)

问题是

weighted_total_score=int()

这将使weighted_total_score为0

应该是

wt_score=weighted_total_score(student_scores)

同样将变量名称从weighted_total_score更改为其他名称,因为该函数已具有该名称

答案 1 :(得分:0)

  

我如何调用weighted_score?

你称之为任何其他方法......

try{
   int a = Integer.parseInt(profile.getTotalBelum());
}catch(NumberFormatException ex){ // handle your exception

注意不要为变量或参数def overall_grade(scores): score = weighted_total_score(scores) 命名,因为您已经有一个具有该名称的方法。如果您引用了本地变量,他们会shadow该方法,这通常不好并且会导致初学者混淆。

你得到F的原因是因为weighted_total_scoreweighted_total_score=int()相同,你的if语句一直到底。

另外,小费,你实际上并不需要在你的条件中同时使用这两个边界,因为这个条件可以通过&#34;落后&#34;。

并提出建议,尝试编写简单的方法,然后在它们之上构建。不要马上做太多事。例如,创建一个只返回字母等级的方法,然后使用打印字符串的方法并使用其他方法的结果。

weighted_total_score=0