编写一个程序,要求用户输入五个测试分数。程序应为每个分数和平均考试分数显示字母等级。
在程序中编写以下功能:
calc_average。此函数应接受五个测试分数作为参数,并返回分数的平均值。
determine_grade。此函数应接受测试分数作为参数,并根据以下评分返回分数的字母评分
规模: 得分字母等级 90–100 A 80–89乙 70–79摄氏度 60–69天 低于60 F
def main():
test_1=int(input("Enter your score for test 1:\n"))
print("Your grade is an",determine_grade(test_1))
test_2=int(input("Enter your score for test 2:\n"))
print("Your grade is an",determine_grade(test_2))
test_3=int(input("Enter your score for test 3\n"))
print("Your grade is an",determine_grade(test_3))
test_4=int(input("Enter your score for test 4\n"))
print("Your grade is an" ,determine_grade(test_4))
test_5=int(input("Enter your score for test 5\n"))
print("Your grade is an",determine_grade(test_5))
average=calc_average(test_1,test_2,test_3,test_4,test_5)
print("The average of the test is",average)
def calc_average(test_1,test_2,test_3,test_4,test_5):
total=test_1+ test_2+ test_3+ test_4+ test_5
average=total/12
return average
def determine_grade(test_score):
if test_score >= 90 and test_score <= 100:
return "A"
elif test_score >= 80 and test_score <= 89:
return "B"
elif test_score >= 70 and test_score <= 79:
return "C"
elif test_score >= 60 and test_score <= 69:
return "D"
else:
return "F"
main()
答案 0 :(得分:2)
它返回您想要的吗?如果是,则您的代码有效!但是,我在这里看到一个错误:
def calc_average(test_1,test_2,test_3,test_4,test_5):
total=test_1+ test_2+ test_3+ test_4+ test_5
average=total/5 # HERE it's divided by 5 not 12 as there are 5 grades
return average
最佳
答案 1 :(得分:0)
您的代码可以正常工作,但有一个逻辑错误。
您需要更改平均计算公式average=total/5
,而不是average=total/12
。因为平均值是总共5门课程的平均值。
答案 2 :(得分:0)
您的代码工作(平均水平除外),其他人指出。这可能是使用循环的简化:
$('#createData > tbody > tr').each(function(i) {
var rowobj = new Object();
var $tds = $(this).find('td'),
Number = $tds.eq(0).val(),
Quantity = $tds.eq(1).val(),
date = $tds.eq(2).val(),
Value = $tds.eq(3).val(),
Containers = $tds.eq(4).val(),
typeOfContainer = $tds.eq(5).val();
rowobj.Number = Number;
rowobj.Quantity = parseFloat(Quantity);
rowobj.Date = eta;
rowobj.alue = parseFloat(Value);
rowobj.noofContainers = parseInt(Containers);
rowobj.containerType = typeOfContainer;
Details.push(rowobj);
});