我正在努力使该功能在我的代码上起作用。我缺少要打印字母等级的部分

时间:2019-02-19 00:18:40

标签: python

编写一个程序,要求用户输入五个测试分数。

假设将输入有效分数,并且将分别输入每个数字,即您将需要5个变量。程序应为每个分数和平均考试分数显示字母等级。在程序中编写以下函数:

main-要求用户分别输入五个测试分数,并将其放入五个float变量中。 main然后应调用showScores 5次,每次传递每个分数之一。当从showScores返回时,main应该调用calcAverage并传递5分。

showScores –接收一个分数并将分数打印到控制台(无需开始新行),并将刚刚打印的分数发送到printLetterGrade,后者将在同一行上打印字母等级。

printLetterGrade-接受一个数字作为参数并显示分数的字母等级 根据以下评分等级:

Score Letter Grade
90-100 A
80-89 B
70-79 C
60-69 D
Below 60 F

calcAverage-接收5个分数作为参数,并显示分数的平均值,以及与该平均值相等的字母等级(利用printLetterGrade函数通过将计算所得的平均值传递给字母来显示字母)。

这是该程序的示例运行:

Enter grade 1: 65
Enter grade 2: 80
Enter grade 3: 90
Enter grade 4: 71
Enter grade 5: 85
65 is D
80 is B
90 is A
71 is C
85 is B
The average is: 78.2 which is C

这是我到目前为止所做的,但是我在某些部分上很挣扎,所以我得到了显示的输出。这是我到目前为止所做的。

def main():
    grade1 = float(input("Enter grade 1:"))
    grade2 = float(input("Enter grade 2:"))
    grade3 = float(input("Enter grade 3:"))
    grade4 = float(input("Enter grade 4:"))
    grade5 = float(input("Enter grade 5:"))
    showScores(grade1, grade2, grade3, grade4, grade5)
    calcAverage(grade1, grade2, grade3, grade4, grade5)

def showScores(grade1, grade2, grade3, grade4, grade5):
    print(grade1)printLetterGrade
    print(grade2)printLetterGrade
    print(grade3)printLetterGrade
    print(grade4)printLetterGrade
    print(grade5)printLetterGrade

def printLetterGrade(showScores):
    if(grade < 60):
        return printLetterGrade == "F"
    elif(grade < 70):
        return printLetterGrade == "D"
    elif(grade < 80):
        return  printLetterGrade == "C"
    elif(grade < 90):
        return  printLetterGrade == "B"
    elif(grade < 101):
        return printLetterGrade == "A"
    return printLetterGrade


def calcAverage(grade1, grade2, grade3, grade4, grade5):
    average = (grade1 + grade2 + grade3 + grade4 + grade5)/ 5
    print("The average is {}".format(average))

main()

1 个答案:

答案 0 :(得分:0)

您需要按以下方式调整showScoresprintLetterGrade的功能:

def showScores(grade1, grade2, grade3, grade4, grade5):
    print("{} is {}".format(grade1, printLetterGrade(grade1)))
    print("{} is {}".format(grade2, printLetterGrade(grade2)))
    print("{} is {}".format(grade3, printLetterGrade(grade3)))
    print("{} is {}".format(grade4, printLetterGrade(grade4)))
    print("{} is {}".format(grade5, printLetterGrade(grade5)))


def printLetterGrade(grade):
    if (grade < 60):
        printLetterGrade = "F"
    elif (grade < 70):
        printLetterGrade ="D"
    elif (grade < 80):
        printLetterGrade = "C"
    elif (grade < 90):
        printLetterGrade = "B"
    elif (grade < 101):
        printLetterGrade = "A"
    return printLetterGrade