[Tutor] Counter not defined error

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Feb 21 18:07:46 CET 2015


On 21/02/2015 16:48, Tim Johnson wrote:

I've changed the subject line to reflect your question.  Did you have to 
reply to the digest and include the lot of it, why couldn't you simply 
start a new thread?

> Hi Guys,
> Very simple question, I imagine.
>
> this code throws of off a "counter not defined error".
> Can you help?
>
> *def word_counter(word, string):*
> *    counter = 0*
> *    for item in string:*
> *        if item == word:*
> *            counter = counter + 1*
> *print counter*
>
> Tim Johnson
>

counter only exists within the scope of the word_counter function, the 
print statement is outside of that scope.  Shifting the print statement 
right four spaces should do the trick.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence



More information about the Tutor mailing list