【问题标题】:I need help finishing a grade calculator program in python我需要帮助在 python 中完成一个成绩计算器程序
【发布时间】:2015-11-22 01:23:31
【问题描述】:

对于我的计算机科学课,我必须编写一个计算成绩的程序。 这是我的第一堂计算机科学课(没有经验),所以我正在努力学习。

这些是方向:

  1. 询问用户他们课程中的测试、作业、测验和实验室的数量。

  2. 询问用户是否存在与上述测试不同的重量,例如一个课程有 2 次测试,每一次占 12.5%,最后一次占 15%。

  3. 对于数字 > 0 的每个类别

    一个。提示用户输入加权百分比,不超过 100%,应该总计 所有类别 100%!!!

    b.获取类别的分数。

    c。如果类别是实验室,则将所有分数相加。

    d。否则,将分数平均。

    e。计算类别的加权平均值。

  4. 使用每个类别的加权平均值,计算课程中的成绩。

  5. 询问用户他/她是否想计算另一个班级的成绩。

  6. 如果用户回答是,则返回步骤 1。

  7. 否则,结束程序。

到目前为止我的代码:

def main():
    lists = get_user_input()
    get_scores(lists);
    get_weighted_average(lists)

def get_user_input():
#   How many?
    t = int(input("How many tests?: "))
    a = int(input("How many assignments?: "))
    q = int(input("How many quizzes?: "))
    l = int(input("How many labs?: "))

#   How much weight on grade?
    tw = float(input("Enter weight of tests: "))
    aw = float(input("Enter weight of assignments: "))
    qw = float(input("Enter weight of quizzes: "))
    lw = float(input("Enter weight of labs: "))
    lists = [t, a, q, l, tw, aw, qw, lw]
    return lists

def get_scores(lists):
    #   What are the scores?
    scores = [0] * 5
    for(x in range(lists[0]):
        test_scores = float(input("enter your test scores: "))
        scores[x] = test_scores
    for(x in range(lists[1]):
        test_scores = float(input("enter your assignment scores: "))
        scores[x] = assignment_scores
    for(x in range(lists[2]):
        test_scores = float(input("enter your quiz scores: "))
        scores[x] = quiz_scores
    for(x in range(lists[3]):
        test_scores = float(input("enter your lab scores: "))
        scores[x] = lab_scores
    sumlabs = 0
    for(x in range(lists[3]):
        sumlabs = sumlabs + scores[x]
    print(sumlabs)

def get_weighted_average(lists):

main()

我不确定如何继续,因此非常感谢任何帮助。

【问题讨论】:

  • 请将您拥有的代码添加为 sn-p,而不是屏幕截图,并说明您目前卡在哪里。请从任务的角度考虑做什么,以及从方法的角度如何解决这些任务。如果您已经完成了这些步骤,那么实施应该会很简单。
  • 我不知道如何获得测试、作业等的平均值。

标签: python python-3.x computer-science


【解决方案1】:

平均分数意味着将它们相加并除以它们的数量。您可以使用列表有办法告诉您它有多少元素这一事实。例如,如果 x 是一个列表,则 len(x) 是列表中事物的数量。您应该小心不要尝试计算空列表的分数,因为这意味着除以零。在以下函数中,如果列表中有内容,“if score_list:”将为真。否则返回 None(因为没有定义平均值)。

def average_score(score_list):
    if score_list:               # makes sure list is not empty
       total = 0                 # our total starts off as zero
       for score in score_list:  # starts a loop over each score
          total += score         # increases the total by the score
       return total / len(score_list)  # returns aveage

要根据用户输入形成一个分数列表,假设用户在提示时会将它们全部放在同一行,则可以从用户那里获取一个字符串(大概像:“13.4 12.9 13.2”,然后使用string 的 split 方法将其转换为 ["13.4", "12.9", "13.2"] 之类的字符串列表,然后将此字符串列表转换为浮点数列表。最后使用上面的平均函数可以得到平均值:

test_score_entry = raw_input("Enter your test scores separated by spaces: ")
test_sore_strings = test_score_entry.split()
test_scores = [float(_) for _ in test_score_strings]
average_score = average(test_scores)

【讨论】:

  • 另外,您的代码 sn-p 有几个语法错误,我猜您已经消除了,但是如果您在阅读本文时仍然卡在语法上,请告诉我。
猜你喜欢
  • 2020-02-10
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多