[Tutor] Local variables and functions

Ryan Smith rcsmith at speakeasy.net
Mon Nov 10 21:54:37 EST 2003

Hello All,

I have been reading "How To Think Like a Computer Scientist" to learn how to
program in python since I am a complete newbie.  I am having problems with
declaring variables.  Namely when they are local to a function.  Below is an
example that I have been working on:

def counterLetters(fruit):
 	fruit = "bananna"
 	count = 0
 	for char in fruit:
 		count = count + 1
 	print count
print counterLetters(fruit) 	

If I make the fruit variable global the function works, however when I have
it local to the function, I get a 
    print counterLetters(fruit)
NameError: name 'fruit' is not defined, error message.  Can someone please
point out what I maybe missing?  The concept of local variables seems pretty
straight forward, but I have been struggling with this issue since the 4th
chapter.  I tried searching on google to see if someone else was having a
similar problem, but to no avail.

Thank you

Ryan Smith

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 1972 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20031110/1d2903cc/winmail.bin

More information about the Tutor mailing list