![](/img/trans.png)
[英]How can i count the amount of each grade in each category as a set of marks
[英]How do I print the amount of marks above the grade boundary and marks below the next?
我必须通过打印正确的等级来编写非常正确的代码,它只打印等级:U,然后也是正确的等级,我不知道为什么会这样。
grades = ["A*", "A", "B", "C", "D", "E", "U"]
mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
mark3 = int(input("Programming project score /70: "))
totalScore = mark1 + mark2 + mark3
print("Total:", totalScore, "/350")
gradeScore = {284 <= totalScore <= 350:grades[0],
246 <= totalScore < 284:grades[1],
208 <= totalScore < 246:grades[2],
171 <= totalScore < 208:grades[3],
134 <= totalScore < 171:grades[4],
97 <= totalScore < 134:grades[5],
0 <= totalScore < 97:grades[6]}
for i in gradeScore:
print("Grade: ", gradeScore[i])
然后当我输入分数时,它会正确地加起来,但会打印:
Computer systems score /140: 57
Algorithms and programming score /140: 68
Programming project score /70: 35
Total: 160 /350
Grade: U
Grade: D
我还需要打印高于你所在年级边界的分数和低于下一个年级的分数,如果没有很长的 if 语句,我该怎么做?
它只打印等级:U,然后打印正确的等级,我不知道为什么会这样。
首先,成绩的打印顺序并不总是相同的(即首先是 U,然后是正确的成绩)。 这取决于您的输入是什么。 如果您选择使您获得 A* 等级的输入,则首先打印 A*,然后打印 U。
发生这种情况的原因是因为您正在创建一个只有两个键( True
和False
)的字典,但是您要多次设置它们的值。 当您在 A* 级时,首先将True
键添加到字典中,这就是它首先打印的原因。 但是,当您在任何其他年级时,首先添加键False
,这就是为什么首先打印 U 的原因。
但这还不是结束。 Grade U 始终存在,因为您尝试插入/修改的最后一个键(与键无关)获得值grades[6]
,即 U。
我不知道你为什么采用这种方法,但我要说这不是正确的方法。 下面介绍了一种正确的方法(但不是唯一的方法):
grades = ["A*", "A", "B", "C", "D", "E", "U"]
mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
mark3 = int(input("Programming project score /70: "))
totalScore = mark1 + mark2 + mark3
print("Total:", totalScore, "/350")
minScoreForGrade = [284, 246, 208, 171, 134, 97, 0]
for i, s in enumerate(minScoreForGrade):
if totalScore >= s:
print("Grade:", grades[i])
print("Marks above boundary:", totalScore-s)
if i > 0:
print("Marks for next grade:", minScoreForGrade[i-1]-totalScore)
else:
print("Marks for next grade: --")
break
解释
您创建每个年级所需的最低分数列表。 然后,您可以从最高totalScore
最低分浏览该列表,以找出您的totalScore
首次高于成绩基线的情况。 一旦你找到了,你可以用简单的算术打印你想要的信息,然后你就跳出了循环。
在您的问题中,字典不是适合使用的数据结构。
注意:建议多个if
语句的答案完全是错误的。 他们可能会得到您需要的结果,但这是一种糟糕的编码实践。 它使您的代码混乱,维护起来非常令人沮丧,最重要的是,容易出错。 相反,总是尝试基于简单的逻辑找到 Pythonic 的方法,以获得您想要的结果。
字典中的键必须是唯一的。 在您当前的字典中,键将根据totalScore
分配,因此您将有多个False
和单个True
。 结果只返回一对False
和True
键。
你可以看到不同的分数产生不同的字典:
Total: 150 /350
{False: 'U', True: 'D'}
Total: 0 /350
{False: 'E', True: 'U'}
但是,您可以通过仅打印字典中的True
值来返回正确的等级。 在您的解决方案中,遍历字典将返回字典中的所有值(始终包括False
和True
键)。 只需用简单的print()
语句替换 for 循环:
print("Grade: ", gradeScore[True])
或者,您可以编写函数来返回正确的成绩:
def getGrade(totalScore):
if 284 <= totalScore <= 350:
return "A*"
elif 246 <= totalScore < 284:
return "A"
elif 208 <= totalScore < 246:
return "B"
elif 171 <= totalScore < 208:
return "C"
elif 134 <= totalScore < 171:
return "D"
elif 97 <= totalScore < 134:
return "E"
elif 0 <= totalScore < 97:
return "U"
然后打印成绩:
print("Grade: ", getGrade(totalScore))
我试着稍微完成你的作业,我认为这可能对你有帮助:
首先,您需要在条目上设置条件,因为您有最大和最小标记:
mark1 = int(input("Computer systems score /140: "))
while mark1 not in range(0,141):
print("mark1 out of range!")
mark1 = int(input("Computer systems score /140: "))
mark2 = int(input("Algorithms and programming score /140: "))
while mark2 not in range(0,141):
print("mark2 out of range!")
mark2 = int(input("Computer systems score /140: "))
mark3 = int(input("Programming project score /70: "))
while mark3 not in range(0,71):
print("mark3 out of range!")
mark3 = int(input("Computer systems score /70: "))
现在要获得向量(“等级”、“上限”、“下限”),我提出以下解决方案:
if 284 <= totalScore <= 350:
print("Grade: ", grades[0],"\nMarks above: ", 350-totalScore,"\nMarks below: ", totalScore-284)
elif 246 <= totalScore < 284:
print("Grade: ", grades[1],"\nMarks above: ", 284-totalScore,"\nMarks below: ", totalScore-246)
elif 208 <= totalScore < 246:
print("Grade: ", grades[2],"\nMarks above: ", 246-totalScore,"\nMarks below: ", totalScore-208)
elif 171 <= totalScore < 208:
print("Grade: ", grades[3],"\nMarks above: ", 208-totalScore,"\nMarks below: ", totalScore-171)
elif 134 <= totalScore < 171:
print("Grade: ", grades[4],"\nMarks above: ", 171-totalScore,"\nMarks below: ", totalScore-134)
elif 97 <= totalScore < 134:
print("Grade: ", grades[5],"\nMarks above: ", 134-totalScore,"\nMarks below: ", totalScore-97)
elif 0 <= totalScore < 97:
print("Grade: ", grades[6],"\nMarks above: ", 97-totalScore,"\nMarks below: ", totalScore)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.