调用函数执行数据树
Calling a function to execute a data tree
我已经有了加权分数代码。
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")
问题是
weighted_total_score=int()
这将使 weighted_total_score 变为 0
应该是
wt_score=weighted_total_score(student_scores)
同时将变量名从 weighted_total_score 更改为其他名称,因为该函数已有该名称
How do i call weighted_score?
您可以像调用任何其他方法一样调用它...
def overall_grade(scores):
score = weighted_total_score(scores)
注意 不要将您的变量或参数命名为 weighted_total_score
,因为您已经有一个使用该名称的方法。如果你引用你的局部变量,他们会 shadow 那个方法,这通常不好并且会给初学者带来困惑。
你得到 F 的原因是因为 weighted_total_score=int()
与 weighted_total_score=0
相同,并且你的 if 语句一直到底部。
此外,提示,实际上您的条件中不需要两个边界,因为条件可以 "fall through"。
还有一个建议,尝试编写简单的方法,然后在它们之上构建。不要一次做太多。例如,制作一个仅 returns 字母等级的方法,然后使用打印字符串并使用其他方法的结果的方法。
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)))
我已经有了加权分数代码。
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")
问题是
weighted_total_score=int()
这将使 weighted_total_score 变为 0
应该是
wt_score=weighted_total_score(student_scores)
同时将变量名从 weighted_total_score 更改为其他名称,因为该函数已有该名称
How do i call weighted_score?
您可以像调用任何其他方法一样调用它...
def overall_grade(scores):
score = weighted_total_score(scores)
注意 不要将您的变量或参数命名为 weighted_total_score
,因为您已经有一个使用该名称的方法。如果你引用你的局部变量,他们会 shadow 那个方法,这通常不好并且会给初学者带来困惑。
你得到 F 的原因是因为 weighted_total_score=int()
与 weighted_total_score=0
相同,并且你的 if 语句一直到底部。
此外,提示,实际上您的条件中不需要两个边界,因为条件可以 "fall through"。
还有一个建议,尝试编写简单的方法,然后在它们之上构建。不要一次做太多。例如,制作一个仅 returns 字母等级的方法,然后使用打印字符串并使用其他方法的结果的方法。
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)))