[Tutor] Printing a list count - Help
Derek Jenkins
derektjenkins at gmail.com
Fri Aug 29 21:17:13 CEST 2014
Hi everybody,
I have a list that I want to go through and finally print a total
count of particular items. In this case, I want to print the result of
how many A's and B's are in the list.
honor_roll_count = 0
student_grades = ["A", "C", "B", "B", "C", "A", "F", "B", "B", "B", "C", "A"]
for grades in student_grades:
honor_roll_count = honor_roll_count + 1
if grades == "A" or grades == "B":
print honor_roll_count
The above code prints 8 lines, each being an entry for which item in
the list was either A or B. Again, I'm looking for the result to be
the number 8 itself - the total number of instances that A or B occurs
in the list.
I'm sure there are a handful of easy solutions for this, I just
haven't hit the right combo yet and I thought I'd reach out to the
tutor list for a bit of advice.
Please and thank you!
More information about the Tutor
mailing list