Alex, Thanks for taking this one step further! I do appreciate it... +1
On Fri, Aug 29, 2014 at 3:48 PM, Alex Kleider <aklei...@sonic.net> wrote: > On 2014-08-29 12:17, Derek Jenkins wrote: >> >> 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. > > > Try the following: > print("Running Python3 script: 'tutor.py'.......") > > > student_grades = ["A", "C", "B", "B", "C", "A", "F", > "B", "B", "B", "C", "A"] > > grades = {} > > for grade in student_grades: > grades[grade] = grades.get(grade, 0) + 1 > > for grade in sorted(grades.keys()): > print("'{}': {}".format(grade, grades[grade])) > > If you are using Python 2, I believe the get method is called something > else; you can look it up if need be. > > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor