给定 4 个测试分数,计算 Python 中的最终字母等级
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25756271/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate final letter grade in Python given 4 test scores
提问by Regina
The question I have is asking me about final grades given 4 parameters (3 tests worth 20% and 1 final worth 40%) scored on a scale of 0 to 100 points. The function is supposed to be as simple as printing finalGrade(test1, test2, test3, final) which will output a letter grade depending on the score. I don't need to have the user input anything however I'm going to experiment once I have this basic code figured out.
我的问题是问我关于给定 4 个参数(3 个测试占 20%,1 个最终占 40%)的最终成绩,评分范围为 0 到 100 分。该函数应该像打印 finalGrade(test1, test2, test3, final) 一样简单,它将根据分数输出字母等级。我不需要让用户输入任何内容,但是一旦我弄清楚了这个基本代码,我就会进行实验。
This is my code so far:
到目前为止,这是我的代码:
def grader(testOne, testTwo, testThree, finalExam):
    first = testOne * .20
    second = testTwo * .20
    third = testThree * .20
    fourth = finalExam *.40
    finalGrade = first + second + third + fourth
    return finalGrade
def ?? (??):
    if grade >= 90 and <= 100:
        return("You received an A")
    elif grade >= 80 and < 90:
        return("You received a B")
    elif grade >= 70 and < 80:
        return("You received a C")
    elif grade >= 60 and < 70:
        return("You received a D")
    else grade < 60:
        return("Sorry, you received an F")
print(testOne, testTwo, testThree, finalExam) #will replace with values
采纳答案by Wyetro
This:
这个:
def ?? (??):
    if grade >= 90 and <= 100:
        return("You received an A")
    elif grade >= 80 and < 90:
        return("You received a B")
    elif grade >= 70 and < 80:
        return("You received a C")
    elif grade >= 60 and < 70:
        return("You received a D")
    else grade < 60:
        return("Sorry, you received an F")
Would be:
将是:
def gradeScores(FinalGrade):
    if FinalGrade >= 90 and FinalGrade <= 100:
        return("You received an A")
    elif FinalGrade >= 80 and FinalGrade < 90:
        return("You received a B")
    elif FinalGrade >= 70 and FinalGrade < 80:
        return("You received a C")
    elif FinalGrade >= 60 and FinalGrade < 70:
        return("You received a D")
    else:
        return("Sorry, you received an F")
And:
和:
print(testOne, testTwo, testThree, finalExam) #will replace with values
Would be:
将是:
print(gradeScores(grader(testOne, testTwo, testThree, finalExam))) #will replace with values
回答by Paul Becotte
Some comments-
一些评论——
A. Your second function, you use the variable "grade" ... that's a pretty good clue that "grade" should be the argument to the function. You would try to name the function based on what it actually does... in this case, translate a number grade to a string.
A. 你的第二个函数,你使用变量“grade”……这是一个很好的线索,“grade”应该是函数的参数。您会尝试根据函数的实际作用来命名该函数……在这种情况下,将数字等级转换为字符串。
B. The "main" part of your script (where you call 'print') is lacking. You don't need to actually access any of the internal parts of your functions from here- you pass values in and use the result. Something like
B. 缺少脚本的“主要”部分(您称之为“打印”)。您不需要从这里实际访问函数的任何内部部分 - 您传递值并使用结果。就像是
grade1 = 90
...
finalgrade = grader(grade1, grade2, grade3, grade4)
result = translategrade(finalgrade)
print(result)
is what you are looking for there. You'll notice you don't access any of the private variables globally- you pass variables in, you use the return value on the way out.
是你在那里寻找的东西。你会注意到你没有全局访问任何私有变量——你传入变量,你在出路时使用返回值。
回答by user7539791
while True:
为真:
print("What is your Score out of 80?")
score = input()
first = int(input("What was your mark:"))
Marks = int(input("Enter what the test was out of :"))
result = first / Marks * 100
print(first ,"/" ,Marks, '*' ,100 ,"=" , result, "%" )
if result > 90:
    print("You received an A*")
elif result > 70 and 90 :
    print("You received a A")
elif result > 60 and 70 :
    print("You received a B")
elif result > 50 and 60 :
    print("You received a C")
elif result < 50:   
    print("You received an D, and have Failed")
ans = no
input("Would you like to continue using the program?")
回答by Sebastian Scholl
I know this is an old thread, but wanted to see how others were handling this and offer my own solution.
我知道这是一个旧线程,但想看看其他人如何处理这个问题并提供我自己的解决方案。
First, if you have a list of grades, first find the average.
首先,如果您有成绩列表,请先找到平均值。
# GREADES = [85, 92, 96, 67, 73]
def numerical_grade(grades):
    return (sum(grades) / float(len(grades))) / 100
Divide it by ten to get a float between 0 and 1. Then to get a letter grade, simply pull an index from the grade list by subtracting the grade from 10.
将其除以 10 得到 0 和 1 之间的浮点数。然后要获得字母等级,只需通过从 10 中减去等级来从等级列表中提取一个索引。
# GRADE = 0.75
def letter_grade(grade):
    grades = 'ABCDFFFFFF'
    if grade > 0:
        return grades[10 - ceil(grade * 10)]
    else
        return 'F'

