我正在学习 python 并正在研究这个问题,但似乎无法让它发挥作用。执行此代码时,我在不同的行上得到数字 1 到 8。我怎样才能只打印 8 的计数?
honor_roll_count = 0
student_grades = ["A", "C", "B", "B", "C", "A", "F", "B", "B", "B", "C", "A"]
for grade in student_grades:
if grade in "AB":
honor_roll_count = honor_roll_count + 1
print honor_roll_count
最佳答案
您的 print
是缩进的,这意味着它在 for
循环内的 if
内,因此它发生在每个“A”或“乙”。你想要在循环之后得到它。
for grade in student_grades:
if grade in "AB":
honor_roll_count = honor_roll_count + 1
print honor_roll_count
关于python - 试图获得总和,但似乎没有用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18243337/