Fall 2006 Midterm 2 Review: String Stuff
Post your questions here.
def countAs(string):
count=0
for i in string:
if i=="a":
count=count+1
print count
def countVowels(string):
vowels="aeiou"
count=0
for i in string:
for v in vowels:
if i==v:
count=count+1
print count
Jeffrey Wells
| The code is right except you need to do 'return count' instead of 'print count' in both the cases. - Bobby Mathew |
def countAs(string):
count = string.count(“a”)
return count
| Yup, that's another way to do it. But make sure you know how to do it with loops as well. - Bobby Mathew |
2.
def countAs (string):
value=0
for i in list:
if i =="a" or i=="e" or i=="i" or i=="o" or i=="u"
value=value+1
return value
Stacy Schwaiger
| Your loop should be 'for i in string' and there should be a ':' after your 'if' statement. - Bobby Mathew |
Link to this Page