gpt4 book ai didi

python - 硬件问题 : Is it possible to convert all the test scores without additional blocks in grading function?

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:28 26 4
gpt4 key购买 nike

我有这个家庭作业问题要求输入 5 个考试分数并计算等效的字母等级,然后计算平均值并对其应用字母等级。

我目前有一个工作程序,但我想看看是否有可能通过仅使用一组“if”语句来获得所有分数

def letter_grade(test1, test2, test3, test4, test5, average):
if test1 >= 90 and test1 <=100:
score1 = "A"
elif test1 >= 80 and test1 <= 89:
score1 = "B"
elif test1 >= 70 and test1 <= 79:
score1 = "C"
elif test1 >= 60 and test1 <= 69:
score1 = "D"
elif test1 < 60:
score1 = "F"


if test2 >= 90 and test2 <=100:
score2 = "A"
elif test2 >= 80 and test2 <= 89:
score2 = "B"
elif test2 >= 70 and test2 <= 79:
score2 = "C"
elif test2 >= 60 and test2 <= 69:
score2 = "D"
elif test2 < 60:
score2 = "F"


if test3 >= 90 and test3 <=100:
score3 = "A"
elif test3 >= 80 and test3 <= 89:
score3 = "B"
elif test3 >= 70 and test3 <= 79:
score3 = "C"
elif test3 >= 60 and test3 <= 69:
score3 = "D"
elif test3 < 60:
score3 = "F"


if test4 >= 90 and test4 <=100:
score4 = "A"
elif test4 >= 80 and test4 <= 89:
score4 = "B"
elif test4 >= 70 and test4 <= 79:
score4 = "C"
elif test4 >= 60 and test4 <= 69:
score4 = "D"
elif test4 < 60:
score4 = "F"



if test5 >= 90 and test5 <=100:
score5 = "A"
elif test5 >= 80 and test5 <= 89:
score5 = "B"
elif test5 >= 70 and test5 <= 79:
score5 = "C"
elif test5 >= 60 and test5 <= 69:
score5 = "D"
elif test5 < 60:
score5 = "F"


if average >= 90 and average <=100:
avgScore = "A"
elif average >= 80 and average <= 89:
avgScore = "B"
elif average >= 70 and average <= 79:
avgScore = "C"
elif average >= 60 and average <= 69:
avgScore = "D"
elif average < 60:
avgScore = "F"
return score1, score2, score3, score4, score5, avgScore

如果可能的话,我希望函数以更有效的方式返回字母分数。

最佳答案

只需定义函数,然后使用它对每个测试进行评分和平均分:

def letter_grade(score):
if 90 <= score <= 100:
grade = "A"
elif 80 <= score <= 89:
grade = "B"
elif 70 <= score <= 79:
grade = "C"
elif 60 <= score <= 69:
grade = "D"
elif score < 60:
grade = "F"
return grade


tests = [50, 60, 65, 99, 25]

for test in tests:
print(f"score: {test}, grade: {letter_grade(test)}")

print("average grade:", letter_grade(sum(tests) / len(tests)))

Output:

score: 50, grade: F
score: 60, grade: D
score: 65, grade: D
score: 99, grade: A
score: 25, grade: F
average grade: F

关于python - 硬件问题 : Is it possible to convert all the test scores without additional blocks in grading function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55444713/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com