Midterm Exam 2 Review Fall 2004: Count 'em up
Post answers, comments, and questions here!
(Back to Fall2004 Midterm 2 Review)
This is what I think to be the solution for part b:
def countAsBsCs("string"):
count=0
for i in "string":
if i=="1" or i=="2":
count=count+1
print count
Victor Du
| If the string as A's and B's in it, why would you look for 1's and 2's? Why are you putting quotes around the variable "string"? Mark Guzdial |
def countAs(string):
count = 0
for letter in string:
if letter == "a":
count = count + 1
print count
def CountAsBsCs(string):
count = 0
count1 = 0
count2 = 0
for letter in string:
if letter == "a":
count = count + 1
if letter == "b":
count1 = count1 + 1
if letter == "c":
count2 = count2 + 1
print "as =", count
print "bs =", count1
print "cs =", count2
Julie Champion
| Do see FAQ for how to post code so that we can all read it, please. Mark Guzdial |
def count(string):
print "Number of As = ",string.count('a')
print "Number of Bs = ",string.count('b')
print "Number of Cs = ",string.count('c')
Link to this Page