0

Python newbie here. I am writing a program to count how many times a substring is a string, but keep receiving the error message: TypeError: a is undefined. I have looked at /googled similar threads but the code is usually convoluted and hard to follow.

Here is my code:

def stringcount(char, word):
for eachChar in word:
    if char==eachChar:
        count=count+1
        return count
    print count

stringcount('a', 'apple')

Thanks a lot!

9
  • 3
    In case you don't know you can also call the function count on string, e.g. 'apple'.count('a') Commented Jul 25, 2012 at 19:41
  • Your return statement is going to short circuit your function... also, is your indentation correct here? Commented Jul 25, 2012 at 19:42
  • @sr2222 is right - and your indentation is messed up. Commented Jul 25, 2012 at 19:42
  • ...and count isn't initialized! Commented Jul 25, 2012 at 19:44
  • This is not the code you are using. It cannot produce the error you describe. However, this code uses count before assigning it a value, so you'll get UnboundLocalError. Commented Jul 25, 2012 at 19:44

3 Answers 3

4
def stringcount(c, word):
    count = 0
    for eachChar in word:
        if c==eachChar:
            count=count+1

    return count

>>> stringcount('a', 'apple')
1
>>>

But in this case you can simply use:

>>> 'apple'.count('a')
1
Sign up to request clarification or add additional context in comments.

Comments

2

This code is dangerous. You aren't initializing count, so if count exists as a variable in the namespace you could get a different result every time. For example, if you call the function multiple times, count will not reset and the second time the function is called it will return 2. The following code is much safer and works.

def stringcount(char, word):
    count = 0
    for eachChar in word:
        if char==eachChar:
            count += 1
    return count

>>> stringcount('a', 'apple')
1

Also note that you can simply do 'apple'.count('a') to do the same thing.

A nice one-liner does the same thing:

def stringcount(char, word):
    return sum(x==char for x in word)
>>> stringcount('p', 'apple')
2

1 Comment

+1 for emphasizing the need to initialize count within the function namespace.
0

If you aren't writing this for a programming exercise, you could do:

def stringcount(c, word):
    return word.count(c)

print stringcount("a", "apple")

Docs: http://docs.python.org/library/string.html#string.count

4 Comments

Why define a function at all? The OP can just call 'apple'.count('a').
@mutzmatron: Clearly, but I'm following the functional syntax of the OP.
I see what you mean, though I agree with @sr2222 in the sense that if this is some kind of assignment the OP probably has to define their own full function, and otherwise no function is required at all. I don't really see a point in the halfway answer?... but then who knows!
I also forgot about string.count(). Anyway, some people may be more comfortable with functional syntax than object oriented syntax. Never mind Zen 12 :)

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.