程序如下:编写一个程序,要求用户输入5个测试分数。程序应为每个分数和平均考试分数显示字母等级。
我的输出出现问题,显示出每个年级(包括平均分)的第一个输入年级的字母等级。我的数字平均值也被输出为字母等级,但它给出的是正确的平均字母,而不是该等级的正确数字结果。我属于低级编码班,因此程序必须以这种格式编写。
代码如下:
.kitten {
width: 525px;
height: 252px;
background-image: var(--bg-image);
}
每当我输入(a):
<div class="kitten"
style="--bg-image: url('http://placekitten.com/525/252');">
</div>
代替代码中显示的内容,出现语法错误:
a=float(input("Enter score 1:"))
b=float(input("Enter score 2:"))
c=float(input("Enter score 3:"))
d=float(input("Enter score 4:"))
e=float(input("Enter score 5:"))
def determine_grade(a,b,c,d,e):
num=a
if(num<=100 and num>=90):
grade='A'
elif(num<=89 and num>=80):
grade='B'
elif(num<=79 and num>=70):
grade='C'
elif(num<=69 and num>=60):
grade='D'
else:
grade='F'
return grade
def calc_average(a,b,c,d,e):
mean=(a+b+c+d+e)//5
if mean<=100 and mean>=90:
avggrade='A'
elif(mean<=89 and mean>=80):
avggrade='B'
elif(mean<=79 and mean>=70):
avggrade='C'
elif(mean<=69 and mean>=60):
avggrade='D'
else:
avggrade='F'
return avggrade
grade=determine_grade(a,b,c,d,e)
avggrade=determine_grade(a,b,c,d,e)
mean=calc_average(a,b,c,d,e)
determine_grade(a,b,c,d,e)
calc_average(a,b,c,d,e)
print("Score Numeric Grade Letter Grade")
print("--------------------------------------------")
print("Score 1: ",a," ",grade)
print("Score 2: ",b," ",grade)
print("Score 3: ",c," ",grade)
print("Score 4: ",d," ",grade)
print("Score 5: ",e," ",grade)
print("--------------------------------------------")
print("Average Score: ",mean," ",avggrade)
如果我不执行(a)中显示的操作,则输出为:
num=a,b,c,d,e
答案 0 :(得分:0)
好的,所以这里有些事情要谈。首先,我建议您对简单的功能进行更多的实验,以正确地习惯它们。定义determine_grade(a,b,c,d,e)
时,您的函数将希望在函数内部找到a,b,c,d,e参数,但是如果再次阅读它,您会注意到您仅提及a
。这意味着,当您致电grade=determine_grade(a,b,c,d,e)
时,您只是在计算成绩,这就是为什么每个人的成绩都相同(如果您在代码中输入grade
会发现它会输出{{1 }}。
编写可以完全满足您需求的函数的另一种方法是:
'A'
您输入一个分数(可以是a,b,c,d或e),并且它会计算分数。如果要获得每个人的成绩,则可以执行以下操作:
def determine_grade(score):
num=score
if(num<=100 and num>=90):
grade='A'
elif(num<=89 and num>=80):
grade='B'
elif(num<=79 and num>=70):
grade='C'
elif(num<=69 and num>=60):
grade='D'
else:
grade='F'
return grade
这将列出所有年级的列表。
然后,如果要获得平均成绩,最好计算平均分数,因此可以使用相同的函数来获得成绩:
grades=[]
for i in [a,b,c,d,e]:
grades.append(determine_grade(i))
这样做,您将以更少的代码行获得所需的所有信息(并且您可以使用列表推导使效率更高,但这要先进一些)。
答案 1 :(得分:0)
要回答您的问题,应将函数重写为接受单个输入,然后多次调用。同样,您的程序是应该干燥的经典案例(不要重复自己)。除了手动输入a = input()
之外,b = input()
还要考虑不将所有这些输入都放入一个列表中(也许在提示符下使用某些字符串格式,例如我的代码)。下面是一个可以轻松调整以接受任意数量的输入的程序:
input_scores = [] # make our list
scores_input = 5 # tell the program we have 5 scores to record
for input_count in range(1, scores_input + 1):
input_scores.append(float(input("Enter score {}:".format(input_count))))
def determine_grade(percent): # single input
if 90 <= percent <= 100:
grade = 'A'
elif 80 <= percent <= 89:
grade = 'B'
elif 70 <= percent <= 79:
grade = 'C'
elif 60 <= percent <= 69:
grade = 'D'
else:
grade = 'F'
return grade # single output
def calc_average(local_score_list): # expects the list we made earlier (many inputs)
mean = sum(local_score_list) // len(local_score_list)
if 90 <= mean <= 100:
average_grade = 'A'
elif 80 <= mean <= 89:
average_grade = 'B'
elif 70 <= mean <= 79:
average_grade = 'C'
elif 60 <= mean <= 69:
average_grade = 'D'
else:
average_grade = 'F'
return mean, average_grade # have our function return both values, since it calculated them both anyways
compiled_scores = []
for test_score in input_scores:
letter_grade = determine_grade(test_score) # calling our single input/output function many times in a for loop
compiled_scores.append((test_score, letter_grade)) # creating a tuple so each letter and percent is stored together
mean_percent, mean_letter_grade = calc_average(input_scores) # decompile both values from our function
print("Score Numeric Grade Letter Grade")
print("--------------------------------------------")
# now we iterate through all the scores made and print them
for count, result_tuple in enumerate(compiled_scores, 1): # just print the scores in a loop
print("Score {}: ".format(count), result_tuple[0], " ", result_tuple[1])
print("--------------------------------------------")
print("Average Score: ", mean_percent, " ", mean_letter_grade)
您可以看到我们将input()
行压缩为->一行,并且还压缩了print语句!现在,我们的程序可以得到30分,而且效果很好!!! (这是巨大的)!!唯一需要更改的就是脚本顶部的数字5
-> 30
。我们不再需要键入print('this')
或input('that')
!我们的脚本将自动调整,因为我们使其能够根据列表进行缩放。
我的测试用例(使用帖子中的相同数字):
Enter score 1:90
Enter score 2:88
Enter score 3:76
Enter score 4:68
Enter score 5:40
Score Numeric Grade Letter Grade
--------------------------------------------
Score 1: 90.0 A
Score 2: 88.0 B
Score 3: 76.0 C
Score 4: 68.0 D
Score 5: 40.0 F
--------------------------------------------
Average Score: 72.4 C
如果您需要进一步说明我发布的内容,请告诉我!