0

I have to define a function called letterCount in which it returns an integer for the number of times there is a string in the function. For some reason, it returns 2 and I am supposed to return 7

def letterCount(text,collection):
    for collection in text:
        if collection in text:
           return len(collection) + 1
seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))
6
  • It looks to me like your function does not return anything at all. Commented Feb 20, 2014 at 14:43
  • Also, I think that your indentation is still not correct. The last line is not part of your function is it? Commented Feb 20, 2014 at 14:43
  • for collection in text...if collection in text is kind of redundant. Commented Feb 20, 2014 at 14:44
  • 1
    You're also redefining collection in your function. You start off with collection being "ac" but then, when you do your for loop, collection becomes "T" (i.e. the first letter in your sentence). Commented Feb 20, 2014 at 14:47
  • 1
    Check out this resource: pythontutor.com/visualize.html#. It will execute your code step by step and show you the value of every variable along the way. I found it extremely helpful when I was starting out (and still sometimes today!) Commented Feb 20, 2014 at 14:58

6 Answers 6

2

Maybe you want this:

def letterCount(text,collection):
    res = 0
    for c in text:
        if c in collection:
            res = res + 1
    return res
seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

you define an variable collection in for collection in text:, it hidden the variable in def letterCount(text,collection).

Sign up to request clarification or add additional context in comments.

Comments

0

I think the syntax of the python code you wrote is wrong. this is most probably what you want

def letterCount(text,collection):
  for collection in text:
    if collection in text:
        return len(collection) + 1
seuss = 'The cat in a hat came back'
Letters = 'ac'
print(letterCount(seuss,Letters))

your question is also not clear. can you elaborate a little. if what you want is to count the number of words in the string this would suffice

print len(seuss.split(' '))

1 Comment

I think he wants to count the number of times the letters "a" or "c" appear in his text.
0

If you want to count the number of times the string letters occurs in seuss:

def letterCount(text,collection):
    count = 0

    for i in range(0, len(text) - len(collection)):
        if collection == text[i:i + len(collection)]:
            count += 1

    return count

seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

If you want to count the number of times each letter in letters occurs in seuss:

def letterCount(text,collection):
    count = 0

    for i in range(0, len(text)):
        for c in collection:       
            if c == text[i]:
                count += 1

    return count

seuss = 'The cat in a hat came back'
letters = 'ac'
print(letterCount(seuss,letters))

Comments

0

Assuming you want to count the number of letters in the sentence, I'd do this:

def letterCount(text,collection):
  lettercount = 0
  for letter in collection:
    lettercount += text.count(letter)
  return lettercount


seuss = 'The cat in a hat came back'
Letters = 'ac'
print(letterCount(seuss,Letters)) # Returns 8

Explanation:

lettercount = 0 sets our counter to 0

for letter in collection: creates a loop through the list of the letters you passed to the function. In this case "a" and "c"

lettercount += text.count(letter) counts the number of times each letter appears in the sentence and adds it to the counter.

return lettercount sends the result back.

Comments

0

If you want to find out the number of times either 'a' or 'c' comes in 'The cat in a hat came back', then:

import collections
def letter_count(text, letters):
    counter = collections.Counter(text)
    return sum(counter.get(letter, 0) for letter in letters)

Comments

0

Try this:

import re
def letterCount(text,collection):
    return sum(len(re.findall(c, text)) for c in collection)

# re.findall(c, text) -> find all occurences of letter c in text
# len(...)            -> get the length of the list of all occurrences of c in text
# sum(...)            -> sum up all the partial results

1 Comment

I'll try to comment it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.