找到最低的考试成绩和平均两个高分

问题描述 投票:4回答:2

我在家庭作业中得到了其他一切,但我仍然坚持最后一步,这要求我找到最低的考试成绩,然后显示最高的两个考试成绩的平均值,并显示最低的考试成绩。我知道我需要在“def findAndReturnLowest”下添加一个if / elif / else函数,但我出错了。这是我应该做的enter image description here屏幕截图

这是我的代码

def main():
    score1 = 0.0
    score2 = 0.0
    score3 = 0.0

    score1 = getTestScore()
    score2 = getTestScore()
    score3 = getTestScore()

    calcAvgAndDisplayResults(score1, score2, score3)

def calcAvgAndDisplayResults(s1, s2, s3):
    lowest = 0.0
    average = 0.0
    lowest = findAndReturnLowest(s1, s2, s3)
def findAndReturnLowest(s1, s2, s3):


    average = (s1+s2+s3-lowest)/2
    print("Average = ", average)

def getTestScore():
    test = 0.0
    test=float(input("Enter a test score between 0 and 100: "))
    return test

# start of program
main()
python average display
2个回答
1
投票

使用嵌套的if

def findAndReturnLowest(s1, s2, s3):
    if s1 > s3 and s2 > s3:
        return s3

    else:
        return s2 if s1 > s2 else s1

0
投票

非常简单的方法应该有效,但有很多:

def main():
    score1 = 0.0
    score2 = 0.0
    score3 = 0.0

    score1 = getTestScore()
    score2 = getTestScore()
    score3 = getTestScore()

    calcAvgAndDisplayResults(score1, score2, score3)

def calcAvgAndDisplayResults(s1, s2, s3):
    lowest = findAndReturnLowest(s1, s2, s3)

def findAndReturnLowest(s1, s2, s3 ):
    lowest = min([int(x) for x in [s1,s2,s3]])
    average = (s1+s2+s3-lowest)/2
    print("Average = ", average)
    print("Lowest = ", lowest)

def getTestScore():
    test = 0.0
    test=float(input("Enter a test score between 0 and 100: "))
    return test

# start of program
main()
© www.soinside.com 2019 - 2024. All rights reserved.