用曲线查找当前成绩[Python]

时间:2015-02-18 15:33:14

标签: python

我正在尝试编写一个计算程序,该计划将计算具有不同权重的不同类别成绩的班级中的当前成绩。到目前为止,我有:

import math as m
total_grade = 0

c = int(input('How many curves?'))
for n in range(1,(c+1)):
    print(n)
    curve'{}'.format(n) = float(input('What is curve #{} as a decimal?'.format(n)))
    grade'{}'.format(n) = list(input('What is the {}nth set of grades as a list?'.format(n)))
    partial_grade = curve'{}'.format(n) * m.mean(grade'{}'.format(n))
    total_grade += partial_grade

但我不确定如何制作n个曲线和曲线组以及等级列表。例如,假设有两类成绩:测试和家庭作业。测试曲线为70%,家庭作业曲线为30%。我想首先输入.70和.30,然后为测试输入等级列表,如[.85,.95,。92]和作业,方法相同:[。95,.85,.84,1.0 ]。然后我想找到每个类别的平均值并将其乘以曲线然后将其加到总数中。

1 个答案:

答案 0 :(得分:0)

您需要加权平均值:

tests are   `p1`%          p1 * avg(tests)
homework is `p2`%          p2 * avg(homework)
...
cat_N    is `pN`%          pN * avg(cat_N)
           =====          ================
            100.%          final score

所以

# this code assumes Python 3
def make_weighted_sum_fn(weights):
    """
    Given a list of weights, return a function which
      calculates a final weighted score
    """
    weights = list(weights)
    len_ = len(weights)
    def weighted_sum_fn(values):
        values = list(values)
        assert len(values) == len_
        return sum(w * v for w, v in zip(weights, values))
    return weighted_sum_fn

def average(lst):
    """
    Find the mean value of a list

    eg
        average([1., 2., 6.])   # => 3.

    If the list is empty, return 0.
    """
    if lst:
        return sum(lst) / len(lst)
    else:
        return 0.

def get_curves_fn(N):
    weights = [float(input("What is curve #{}? ".format(n))) for n in range(1, N + 1)]
    return make_weighted_sum_fn(weights)

def get_cat_scores(cat):
    return [
        float(f)
        for f in input("What is the {}th set of grades (space-delimited)? ".format(cat)).split()
    ]

def get_categories_scores(num_categories):
    return [get_cat_scores(cat) for cat in range(1, num_categories + 1)]

def main():
    N   = int(input("How many curves? "))
    cfn = get_curves_fn(N)

    while True:
        name = input("\nEnter student name (or Enter to quit): ").strip()
        if name:
            categories = get_categories_scores(N)
            final = cfn(average(cat) for cat in categories)
            print("{}'s final mark is {}".format(name, final))
        else:
            break

if __name__ == "__main__":
    main()

一样运行
How many curves? 2
What is curve #1? 0.7
What is curve #2? 0.3

Enter student name (or Enter to quit): Jake
What is the 1th set of grades (space-delimited)? 65 75 85
What is the 2th set of grades (space-delimited)? 90 80
Jake's final mark is 78.0

Enter student name (or Enter to quit): Melissa
What is the 1th set of grades (space-delimited)? 88 84 82
What is the 2th set of grades (space-delimited)? 75 78
Melissa's final mark is 82.21666666666667

Enter student name (or Enter to quit):