Python - 课程评分员

时间:2017-09-30 17:35:03

标签: python

我正在为课程评分员编写代码。要求是

"如果平均分数大于或等于70并且没有单个测试分数低于50,则返回"传递"的消息。 如果平均分数低于70或至少有一个考试分数低于50,则返回"失败的消息"。"

一旦确定平均值,它就会给我通过/失败声明。我没看到什么?

所以这是我写的代码:

def course_grader(test_scores):
    average = sum(test_scores)/len(test_scores)
    if average >= 70:
        return "pass"
    else: 
        return "fail"   

def main():
    print(course_grader([100,75,45]))     # "fail"
    print(course_grader([100,70,85]))     # "pass"
    print(course_grader([80,60,60]))      # "fail"
    print(course_grader([80,80,90,30,80]))  # "fail"
    print(course_grader([70,70,70,70,70]))  # "pass"

if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:1)

您没有检查测试分数是否大于或等于50,为此,请使用all()函数评估一行中所有测试分数的条件,如下所示:< / p>

def course_grader(test_scores):
    average = sum(test_scores)/len(test_scores)
    # every ts (test score) must be greater than or equal to 50
    if average >= 70 and all(ts >= 50 for ts in test_scores):
        return "pass"
    else: 
        return "fail

答案 1 :(得分:0)

您还可以检查测试分数的min是否大于或等于50。

def course_grader(test_scores):
    average = sum(test_scores)/len(test_scores)
    if average >= 70 and min(test_scores) >= 50:
        return "pass"
    else: 
        return "fail"