首页 > 解决方案 > 无论输入如何,打印语句默认为第一个“if”语句

问题描述

我对 python 非常陌生,所以这段代码可能是一团糟,但我遇到的问题是无论计算什么平均值,代码都在执行第一个“if”语句(即计算的平均值是76.0,但它仍在执行“A”级,而不是“C”级)。我究竟做错了什么?

此外,为了提供信息,代码应该让用户输入四个测试分数,代码必须在 70-85 范围内随机化第五次测试的分数,计算并返回所有五个测试分数的平均值并分配基于平均值的等级。

def rand():
    import random
    num5 = random.randint(70,85)
    calcAverage(num5)

def calcAverage(num5):
    num1 = float(input("Enter the first test score: "))
    num2 = float(input("Enter the second test score: "))
    num3 = float(input("Enter the third test score: "))
    num4 = float(input("Enter the fourth test score: "))
    print("\nThe random test score generater on your behalf was", num5)
    total = num1 + num2 + num3 +num4 + num5
    average = total // 5
    letterGrade(average)

def letterGrade(average):
    if average >= 90.00 and average <= 100.00:
        print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is A.")

    elif average >= 80.00 and average <= 89.99:
        print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is B.")

    elif average >= 70.00 and average <= 79.99:
        print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is C.")

    elif average >= 60.0 and average <= 69.99:
        print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is D.")

    else:
        print("The average of five test scores is {:.1f}.".format(average),"and the letter grade awarded is F.")

rand()

标签: pythonpython-3.x

解决方案


推荐阅读