Midterm Exam 2 Review Fall 2005: Count 'em up #2
okk, i have this:
def countAs(string):
source=string
source=source.count("a")
return source
but it keeps printing my count, how do i make it to stop printing the count?
am i not supposed to set the dot notation=source?
| This seems like it would work. Please clarify your question. -Blake O'Hare |
well, when u run this in jes, it prints the count(show the actual count number), and the directions say not to print the count/show the count, it just says to return it.
| That's fine. If a function has a return value, it sends it back to the caller of the function which means if you call a function from the command area, it'll return it to the command area and it'll appear to behave exactly like print. As long as there isn't a print or a printNow in your code, you're fine for that particular question. -Blake O'Hare |
and for part b:
def countAs(string):
source=string
source1=source.count("a")
source2=source.count("e")
print source1
print source2
how come i can't do
source=source.count("a").count("e")?
or source=source.count("a","e")?
| source.count(whatever) returns an integer. So when you put the 2nd .count on it, you're actually trying to invoke .count on an integer. This makes the green snake unhappy. .count also only counts one thing at a time, since it can only have 1 return value. So no comma separated argument lists allowed in this case. -Blake O'Hare |
Are the following okay for parts b and c?
b) def countAE(string):
print string.count('a')
print string.count('e')
c)def countAlphs(string):
for i in "abcdefghijklmnopqrstuvwxyz":
print (string.count(i))
Does this look right for part a?
def countCs(string):
return string.count('a')
would this be correct?
def countAs(baseball):
counter = 0
for x in baseball:
if x == a:
counter = counter +1
return counter
def countAlpha(string):
alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for l in alphabet:
lvalue=string.count(l)
print "There are "+str(lvalue)+" "+str(l)+"'s."
Is this correct for part c?
Link to this Page