我在作业中发现了其他所有内容,但我坚持到最后一步,要求我找到最低的测试分数,然后显示最高的两个测试分数的平均值,也显示最低的分数。我知道我需要在“ 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()
答案 0 :(得分:1)
使用嵌套的def findAndReturnLowest(s1, s2, s3):
if s1 > s3 and s2 > s3:
return s3
else:
return s2 if s1 > s2 else s1
:
{{1}}
答案 1 :(得分: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()