python编程 - 考试和作业的平均分数和印刷成绩

时间:2017-06-15 23:25:46

标签: python

请我尝试编写一个Python程序来计算学生的最终成绩,该课程有两个作业和两个考试。作业价值40%的班级和考试价值60%的班级成绩。该计划应执行以下步骤:

  1. 询问用户的assignment1,assignment2,exam1和exam2成绩。所有成绩均为100分。
  2. 计算作业的平均值。
  3. 计算考试的平均值。
  4. 使用以下公式计算最终成绩: 最终成绩= .4 *作业平均成绩+ .6 *考试平均成绩。
  5. 将最终成绩格式化为2位小数。
  6. 显示一条消息,告诉学生最终成绩是什么。
  7. 这是我的计划:

    from math import *
    
    def main():
    
    
        Assignment1 = eval(input("Please enter the score for Assignment 1: "))
        Assignment2 = eval(input("Please enter the score for Assignment 2: "))
        Assignment_total = Assignment1 + Assignment2
        Assignment_average = Assignment_total/2
        print("The average of the assignment is", round(Assignment_average, 2))
    
        Exam1 = eval(input("Please enter the score for Exam 1: "))
        Exam2 = eval(input("Please enter the score for Exam 2: "))
        Exam_total = Exam1 + Exam2
        Exam_average = Exam_total/2
        print("The average of the Exam is", round(Exam_average, 2))
    
    
        Final_grade = 0.4 * Assignment_average + 0.6 * Exam_average
    
        if 90 <= Final_grade <= 100:
            return 'A'
        elif 80 <= Final_grade <= 89:
            return 'B'
        elif 70 <= Final_grade <= 79:
            return 'C'
        elif 60 <= Final_grade <= 69:
            return 'D'
        else:
            return 'F'
    
    main()
    

    我无法打印出成绩。请帮帮我

3 个答案:

答案 0 :(得分:0)

接受我的建议和@ Toad22222,并摆脱那可怕的eval

from math import *

def main():
    Assignment1 = int(input("Please enter the score for Assignment 1: "))
    Assignment2 = int(input("Please enter the score for Assignment 2: "))
    Assignment_total = Assignment1 + Assignment2
    Assignment_average = Assignment_total/2
    print("The average of the assignment is", round(Assignment_average, 2))

    Exam1 = int(input("Please enter the score for Exam 1: "))
    Exam2 = int(input("Please enter the score for Exam 2: "))
    Exam_total = Exam1 + Exam2
    Exam_average = Exam_total/2
    print("The average of the Exam is", round(Exam_average, 2))


    Final_grade = 0.4 * Assignment_average + 0.6 * Exam_average
    print("The final grade is", round(Final_grade, 2))

    if 90 <= Final_grade <= 100:
        print('A')
    elif 80 <= Final_grade <= 89:
        print('B')
    elif 70 <= Final_grade <= 79:
        print('C')
    elif 60 <= Final_grade <= 69:
        print('D')
    else:
        print('F')

main()

<强>更新

只是为了好玩而改写。随意忽略或获取灵感或提出问题。

import collections

Component = collections.namedtuple("Component", ["name", "count", "weight"])

def get_average(name, how_many):
    return sum(
        int(input("Please enter the score for {} {}: ".format(name, i+1)))
        for i in range(how_many)
    ) / how_many

def main():
    components = [
        Component(name="assignment", count=2, weight=0.4),
        Component(name="exam", count=2, weight=0.6),
    ]

    total = 0
    for component in components:
        average = get_average(component.name, component.count)
        print("The average of the {}s is: {:.2f}".format(component.name, average))
        print()
        total += average * component.weight

    letters = [(90, "A"), (80, "B"), (70, "C"), (60, "D")]
    grade = next((letter for score, letter in letters if total >= score), "F")

    print("The final grade is: {:.2f} ({})".format(total, grade))

main()

答案 1 :(得分:0)

您需要通过将其分配给变量然后打印来调用该函数。见下文:

from math import *

def main():
    Assignment1 = eval(input("Please enter the score for Assignment 1: "))
    Assignment2 = eval(input("Please enter the score for Assignment 2: "))
    Assignment_total = Assignment1 + Assignment2
    Assignment_average = Assignment_total/2
    print("The average of the assignment is", round(Assignment_average, 2))

    Exam1 = eval(input("Please enter the score for Exam 1: "))
    Exam2 = eval(input("Please enter the score for Exam 2: "))
    Exam_total = Exam1 + Exam2
    Exam_average = Exam_total/2
    print("The average of the Exam is", round(Exam_average, 2))


    Final_grade = round(0.4 * Assignment_average + 0.6 * Exam_average)

    if 90 <= Final_grade <= 100:
        return "Your final grade is %s: A" %(Final_grade)
    elif 80 <= Final_grade <= 89:
        return "Your final grade is %s: B" %(Final_grade)
    elif 70 <= Final_grade <= 79:
        return "Your final grade is %s: C" %(Final_grade)
    elif 60 <= Final_grade <= 69:
        return "Your final grade is %s: D" %(Final_grade)
    else:
        return "Your final grade is %s: F" %(Final_grade)

mygrades = main()
print (mygrades)

输出

Please enter the score for Assignment 1: 43
Please enter the score for Assignment 2: 88
The average of the assignment is 65.5
Please enter the score for Exam 1: 90
Please enter the score for Exam 2: 89
The average of the Exam is 89.5
Your final grade is 80: B

答案 2 :(得分:0)

你到处都是。您有return,但您没有返回任何内容(即您必须致电grade = main()然后print(grade)

请看下面的评论:

# nothing you are doing requires the math module
# eval() and round() are built-ins; we dont even need eval()
## from math import *

def main():
    # variable names = short & sweet + meaningful
    a1 = int(input("Please enter the score for Assignment 1: "))
    a2 = int(input("Please enter the score for Assignment 2: "))
    atot = a1 + a2
    aavg = atot / 2
    print ("The average of the assignment is", round(aavg, 2))

    e1 = int(input("Please enter the score for Exam 1: "))
    e2 = int(input("Please enter the score for Exam 2: "))
    etot = e1 + e2
    eavg = etot / 2
    print ("The average of the Exam is", round(eavg, 2))

    fingrd = ((0.4 * aavg) + (0.6 * eavg))

    if (90 <= fingrd <= 100):
        print (fingrd, ': A') # edit: included print format you commented
    # no need to do <= on the upper bounds
    # < upper_bound includes <= your previous lower_bound - 1
    # i.e. 80 <= fingrd < 90 equates to 80 <= fingrd < 90
    elif (80 <= fingrd < 90): 
        print (fingrd, ': B')
    elif (70 <= fingrd < 80):
        print (fingrd, ': C')
    elif (60 <= fingrd < 70):
        print (fingrd, ': D')
    else:
        print (fingrd, ': F')

if __name__ == '__main__':
    main()

输出:

Please enter the score for Assignment 1: 70
Please enter the score for Assignment 2: 100
The average of the assignment is 85.0
Please enter the score for Exam 1: 45
Please enter the score for Exam 2: 87
The average of the Exam is 66.0
73.6 : C