在使用python的函数内打印测试成绩和测试分数平均值



作业是获取5个考试成绩,并使用函数显示相应的字母等级和考试成绩平均值。我不知道我是否在正确的轨道上,我在调用主函数中的其他函数时遇到了麻烦。

def main():
s1 = int(input('Enter score one: '))
s2 = int(input('Enter score two: '))
s3 = int(input('Enter score three: '))
s4 = int(input('Enter score four: '))
s5 = int(input('Enter score five: '))
return s1, s2, s3, s4, s5

test_grade = determine_grade(s1, s2, s3, s4, s5)
print(test_grade)

average_num = calc_average(s1, s2, s3, s4, s5)
print(average_num)

def calc_average(s1, s2, s3, s4, s5):
average = s1 + s2 + s3 +s4 +s5 / 5
return average

def determine_grade(s1, s2, s3, s4, s5):
if (90 <= s1, s2, s3, s4, s5 <= 100):
grade_a = "A"
return grade_a
elif (80 <= s1, s2, s3, s4, s5 <= 89):
grade_b = "B"
return grade_b
elif (70 <= s1, s2, s3, s4, s5 <= 79):
grade_c = "C"
return grade_c
elif (60 <= s1, s2, s3, s4, s5 <= 69):
grade_d = "D"
return grade_d
elif (s1, s2, s3, s4, s5 < 60):
grade_f = "F"
return grade_f
main()

return应位于main函数的末尾

def main():
s1 = int(input('Enter score one: '))
s2 = int(input('Enter score two: '))
s3 = int(input('Enter score three: '))
s4 = int(input('Enter score four: '))
s5 = int(input('Enter score five: '))


test_grade = determine_grade(s1, s2, s3, s4, s5)
print(test_grade)

average_num = calc_average(s1, s2, s3, s4, s5)
print(average_num)
return s1, s2, s3, s4, s5

你在平均值中缺少括号,这就是为什么它给出了错误的平均值

def calc_average(s1, s2, s3, s4, s5):
average = (s1 + s2 + s3 +s4 +s5) / 5
return average

您的if语法在比较所有值时是错误的。

def determine_grade(s1, s2, s3, s4, s5):
if all(item >= 90 and item <= 100 for item in (s1, s2, s3 ,s4 ,s5)):
grade_a = "A"
return grade_a
elif all(item >= 80 and item <= 89 for item in (s1, s2, s3 ,s4 ,s5)):
grade_b = "B"
return grade_b
elif all(item >= 70 and item <= 79 for item in (s1, s2, s3 ,s4 ,s5)):
grade_c = "C"
return grade_c
elif all(item >= 60 and item <= 69 for item in (s1, s2, s3 ,s4 ,s5)):
grade_d = "D"
return grade_d
else :
grade_f = "F"
return grade_f
main()

相关内容

  • 没有找到相关文章

最新更新