给出4个测试分数,计算Python中的最终字母等级

时间:2014-09-10 02:08:49

标签: python function

我的问题是询问关于4个参数的最终成绩(3个测试值20%,1个最终值40%),评分为0到100分。该函数应该像打印finalGrade(test1,test2,test3,final)一样简单,它将根据分数输出字母等级。我不需要让用户输入任何内容,但是一旦我找到了这个基本代码,我就会进行实验。

到目前为止,这是我的代码:

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

4 个答案:

答案 0 :(得分:1)

此:

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")

将是:

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")

print(testOne, testTwo, testThree, finalExam) #will replace with values

将是:

print(gradeScores(grader(testOne, testTwo, testThree, finalExam))) #will replace with values

答案 1 :(得分:1)

一些评论 -

一个。你的第二个功能,你使用变量“等级”...这是一个很好的线索,“等级”应该是函数的参数。您可以尝试根据实际功能命名函数...在这种情况下,将数字等级转换为字符串。

B中。您的脚本的“主要”部分(您称之为'print')缺乏。您无需从此处实际访问函数的任何内部部分 - 您传入值并使用结果。像

这样的东西
grade1 = 90
...
finalgrade = grader(grade1, grade2, grade3, grade4)
result = translategrade(finalgrade)
print(result)

是你在那里寻找的。你会注意到你没有全局访问任何私有变量 - 你传入变量,你在出路时使用返回值。

答案 2 :(得分:1)

我知道这是一个老线程,但想看看别人如何处理这个并提供我自己的解决方案。

首先,如果您有成绩列表,请先找到平均值。

# GREADES = [85, 92, 96, 67, 73]
def numerical_grade(grades):
    return (sum(grades) / float(len(grades))) / 100

除以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'

答案 3 :(得分:0)

而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?")