如何计算平均分数计算器中A,B,C,D和F的数量?

时间:2019-09-23 02:05:44

标签: python function dictionary

我正在尝试编写一个Python脚本,该脚本可根据用户输入的考试数量和考试分数来计算平均分数以及A,B,C,D和F的数量。

对于该函数中当前脚本的一部分,我已经尝试了多种方法: “ def letter_score(分数):” 但是无法为输入的考试成绩分配相应的字母却没有成功。

Error parsing parameter '--parameters': Invalid JSON:
[{"ParameterKey":"name","ParameterValue":"John"},{"ParameterKey":"Occupation","ParameterValue":"Test

我成功找到了平均分,但是字母等级给我带来了麻烦。

2 个答案:

答案 0 :(得分:1)

考虑使用python dictionary存储letter_score的计数

您可以通过以下方式将全0初始化为字典:

letter_score_count = { "A": 0, "B": 0, "C": 0, "D": 0, "F": 0}

使用letter_score关键字在函数return中返回值

    def letter_score(scores):
        if scores >= 90:
            return "A"
        elif scores < 90 and scores >= 80:
            return "B"
        elif scores < 80 and scores >= 70:
            return "C"
        elif scores < 70 and scores >= 60:
            return "D"
        else:
            return"F"

然后,调用letter_score方法,将结果用作字典的键,以在输入分数时增加每个字母分数的计数器

letter_score_count[letter_score(scores)] += 1

最后,您可以打印结果

print("There are " + str(letter_score_count["A"]) + " A's.")

您代码中的修改后版本。希望有帮助

    sum_of_scores = 0
    number_of_exams = int(input("What is the size of the class? "))
    print("Now enter the scores below.")
    letter_score_count = { "A": 0, "B": 0, "C": 0, "D": 0, "F": 0}
    def letter_score(scores):
        if scores >= 90:
            return "A"
        elif scores < 90 and scores >= 80:
            return "B"
        elif scores < 80 and scores >= 70:
            return "C"
        elif scores < 70 and scores >= 60:
            return "D"
        else:
            return"F"

    for i in range(1, number_of_exams + 1):
        scores = int(input("Student %d : " % (i)))
        sum_of_scores += scores
        letter_score_count[letter_score(scores)] += 1

    average_score = sum_of_scores / number_of_exams

    print("The average is " + str(average_score))

    print("There are " + str(letter_score_count["A"]) + " A's.")
    print("There are " + str(letter_score_count["B"]) + " B's.")
    print("There are " + str(letter_score_count["C"]) + " C's.")
    print("There are " + str(letter_score_count["D"]) + " D's.")
    print("There are " + str(letter_score_count["F"]) + " F's.")

答案 1 :(得分:0)

让我知道是否有帮助。您需要在字母得分功能中包括计数逻辑。

from collections import defaultdict

def letter_score(scores,counter_store):
      if scores >= 90:
           scores = "A"
           counter_store["A"]+=1
      elif scores < 90 and scores >= 80:
           scores = "B"
           counter_store["B"]+=1
      elif scores < 80 and scores >= 70:
           scores = "C"
           counter_store["C"]+=1
      elif scores < 70 and scores >= 60:
           scores = "D"
           counter_store["D"]+=1
      else:
           scores = "F"
           counter_store["F"]+=1
      return(counter_store)
sum_of_scores = 0
counter_store=defaultdict(int)
number_of_exams = int(input("What is the size of the class? "))
print("Now enter the scores below.")
for i in range(1, number_of_exams + 1):
     scores = int(input("Student %d : " %(i)))
     score_count=letter_score(scores,counter_store)
     sum_of_scores += scores


average_score = sum_of_scores/number_of_exams
print("The average is " + str(average_score))

print("There are " + str(counter_store["A"]) + " A's.")
print("There are " + str(counter_store["B"]) + " B's.")
print("There are " + str(counter_store["C"]) + " C's.")
print("There are " + str(counter_store["D"]) + " D's.")
print("There are " + str(counter_store["F"]) + " F's.")