无论给定输入如何,Print语句默认为第一个“ if”语句

时间:2019-03-25 01:48:57

标签: python python-3.x

我是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()

0 个答案:

没有答案