测试平均和成绩 - Python

时间:2015-11-01 19:17:32

标签: python

我正在尝试创建一个python程序,当你输入5个测试分数时,它会显示相应的字母等级,然后得到这五个等级的平均值,并显示平均值的字母等级。

这是我的计划:

#Test Average and Grade

#Enter 5 test scores
test1 = int(input('Enter score 1: '))
test2 = int(input('Enter score 2: '))
test3 = int(input('Enter score 3: '))
test4 = int(input('Enter score 4: '))
test5 = int(input('Enter score 5: '))

total =(test1 + test2 + test3 + test4 + test5)

#Calculate average
def calc_average(total):
    return total / 5

#Grading scale
def determine_score(grade):
    if 90 <= grade <= 100:
        return 'A'
    elif 80 <= grade <= 89:
        return 'B'
    elif 70 <= grade <= 79:
        return 'C'
    elif 60 <= grade <= 69:
        return 'D'
    else:
        return 'F'


grade = total
avg = calc_average(total)
abc_grade = determine_score(grade)

print("That's a(n): " + str(abc_grade))
print('Average grade is: ' + str(avg))

我遇到的问题是,在输入每个分数后我无法显示成绩,而我无法弄清楚如何获得字母等级以及在最后一个声明中的平均值任何帮助将不胜感激。

将程序更改为:

 #Test Average and Grade

#Enter 5 test scores
test1 = int(input('Enter score 1: '))
test2 = int(input('Enter score 2: '))
test3 = int(input('Enter score 3: '))
test4 = int(input('Enter score 4: '))
test5 = int(input('Enter score 5: '))

total =(test1 + test2 + test3 + test4 + test5)

    #Calculate average
    def calc_average(total):
        return total / 5

    #Grading scale
    def determine_score(grade):
        if 90 <= grade <= 100:
            return 'A'
        elif 80 <= grade <= 89:
            return 'B'
        elif 70 <= grade <= 79:
            return 'C'
        elif 60 <= grade <= 69:
            return 'D'
        else:
            return 'F'

    grade = total
    avg = calc_average(total)
    abc_grade = determine_score(grade)

    print("Score 1:", determine_score(test1)) 
    print("Score 2:", determine_score(test2)) 
    print("Score 3:", determine_score(test3)) 
    print("Score 4:", determine_score(test4)) 
    print("Score 5:", determine_score(test5)) 
    print("The average grade is: %s which is a(n): %s " % (str(avg), abc_grade))

结果看这个:

输入得分1:75 输入得分2:80 输入得分3:33 输入得分4:90 输入得分5:52 分数1:C 得分2:B 得分3:F 分数4:A 得分5:F 平均成绩为:66.0,即(n):F

尝试获得如下结果: 输入得分1:75 那是(n):C 输入得分2:80 那是(n):B 输入得分3:33 那是(n):C 输入得分4:90 那是(n):A 输入得分5:52 那是(n):F 平均成绩为:66.0,即(n):F

3 个答案:

答案 0 :(得分:0)

要让用户输入分数打印字母等级,您可以执行以下操作:

#Calculate average
def calc_average(total):
    return total / 5

#Grading scale
def determine_score(grade):
    if 90 <= grade <= 100:
        return 'A'
    elif 80 <= grade <= 89:
        return 'B'
    elif 70 <= grade <= 79:
        return 'C'
    elif 60 <= grade <= 69:
        return 'D'
    else:
        return 'F'

#Enter 5 test scores
scores = []
for i in range(1, 6):
    score = int(input('Enter score {0}: '.format(i)))
    print 'That\'s a(n): ' + determine_score(score)
    scores.append(score)

total = sum(scores)
avg = calc_average(total)
abc_grade = determine_score(avg)


print('Average grade is: ' + str(avg))
print("That's a(n): " + str(abc_grade))

字母等级问题是你使用的是所有等级加起来的总数(如果你在所有5个作业中至少得到20个则希望超过100),所以它总是默认为其他。您想使用avg,以便它可以为您提供与平均值相对应的字母等级:

abc_grade = determine_score(avg)

您也可以省略grade = total行,因为在进行此更改后您将永远不会使用成绩。

答案 1 :(得分:-1)

如前所述,如果您想获得正确的成绩单,则需要更改以下代码:

abc_grade = determine_score(grade)

为:

abc_grade = determine_score(avg)

我不能100%肯定你在上一个问题中的意思,在那里你询问如何在平均成绩之后显示成绩字母,但如果我理解正确你可以这样做:

print "The average grade is: %s and the grade letter of the average is: %s " % (str(avg), abc_grade)

答案 2 :(得分:-1)

def main():
    score1 = 0.0
    score2 = 0.0
    score3 = 0.0
    score4 = 0.0
    score5 = 0.0
    avg = 0.0

    score1 = getScore1()
    score2 = getScore2()
    score3 = getScore3()
    score4 = getScore4()
    score5 = getScore5()

    findAvg = calc_average(score1, score2, score3, score4, score5)
    letterGrade = determine_grade(findAvg)

    print('Score\t\t Numeric Grade   Letter Grade')
    print('------------------------------------------------')
    print('Score 1:\t', score1, '\t\t', determine_grade(score1))
    print('Score 2:\t', score2, '\t\t', determine_grade(score2))
    print('Score 3:\t', score3, '\t\t', determine_grade(score3))
    print('Score 4:\t', score4, '\t\t', determine_grade(score4))
    print('Score 5:\t', score5, '\t\t', determine_grade(score5))

    print('Your final average is: ', findAvg, ',', 'This is your letter   grade: ', letterGrade)

def getScore1():
    score1 = float(input('Enter score 1: '))
    return score1
def getScore2():
    score2 = float(input('Enter score 2: '))
    return score2
def getScore3():
    score3 = float(input('Enter score 3: '))
    return score3
def getScore4():
    score4 = float(input('Enter score 4: '))
    return score4
def getScore5():
    score5 = float(input('Enter score 5: '))
    return score5

def calc_average(score1, score2, score3, score4, score5):
    total = score1 + score2 + score3 + score4 + score5
    avg = total / 5
    return avg

def determine_grade(findAvg):
    if findAvg >= 90 and findAvg <= 100:
        grade = 'A'
        return grade
    elif findAvg >= 80 and findAvg <= 89:
        grade = 'B'
        return grade
    elif findAvg >= 70 and findAvg <= 79:
        grade = 'C'
        return grade
    elif findAvg >= 60 and findAvg <= 69:
        grade = 'D'
        return grade
    else:
        grade = 'F'
        return grade
main()