[Tutor] unexpected error

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 9 Oct 2001 11:06:50 -0700 (PDT)


On Tue, 9 Oct 2001, Jerry Lake wrote:

> I'm reading through the eBook
> "how to think like a computer programmer" python version
> and I'm following along in chapter 7.9 "The string Module"
> 
> however I get this error from this code, any ideas ? the output is
> completely not expected
> 
> <snip>
> #!/usr/bin/python2
> 
> import string
> 
> fruit = "banana"
> count = 0
> index = 0
> for char in fruit:
>   if char == 'a':
>     count = count + 1
> print count
> 
> indeX = string.find(fruit, "a")
> print indeX
> </snip>
> 
> <output>
> this is a
> 14
> 1
> 3
> Traceback (most recent call last):
>   File "./fortest.py", line 13, in ?
>     indeX = string.find(fruit, "a")
> AttributeError: 'string' module has no attribute 'find'


??!  Weird!  Let's check one thing.  Can you put:

###
print string.__file__
###

right after you import the string module?  This will show us where Python
is finding the string module.

My best guess so far is that you might have a 'string.py' file in your
current directory, which is obscuring the standard library's string
module.  Here's what happens on my side when I run your snippet:

###
3
1
###


Hope this helps!