python on window

Parthan SR python.technofreak at gmail.com
Mon Mar 26 08:42:30 EDT 2007


On 26 Mar 2007 05:00:54 -0700, sandeep patil <san.gujar at gmail.com> wrote:
>
> i have written this program but i have gott following error,
> in anather proram "indentation error" sir how i will indent in my
> editor
>
> #test.py
> >>> def invert(table):
>         index=()
>         for key in table:
>                 value=table[key]
>                 if not index.has_key(value):
>                         index[value]=[]
>                         index[value].append(key)
>                 return index
>
>
> >>> phonebook = {'sandeep':9325, 'amit':9822, 'anand':9890, 'titu': 9325}
> >>> phonebook
> {'titu': 9325, 'amit': 9822, 'anand': 9890, 'sandeep': 9325}
> >>> print phonebook
> {'titu': 9325, 'amit': 9822, 'anand': 9890, 'sandeep': 9325}
> >>> inverted_phonebook = invert(phonebook)
>
> Traceback (most recent call last):
>   File "<pyshell#13>", line 1, in <module>
>     inverted_phonebook = invert(phonebook)
>   File "<pyshell#9>", line 5, in invert
>     if not index.has_key(value):
> AttributeError: 'tuple' object has no attribute 'has_key'
> >>> interted_phonebook= invert(phonebook)
>
> Traceback (most recent call last):
>   File "<pyshell#14>", line 1, in <module>
>     interted_phonebook= invert(phonebook)
>   File "<pyshell#9>", line 5, in invert
>     if not index.has_key(value):
> AttributeError: 'tuple' object has no attribute 'has_key'
> >>>



In your code, index = () means it is a tuple. IIRC, it should be a
dictionary. For that, index = {}. This is the one causing the following
error in your code,

  if not index.has_key(value):
AttributeError: 'tuple' object has no attribute 'has_key'

You can not use has_key over a tuple object.

But where is the "indentation error" ? I see nothing like that in the error
message.


-- 
With Regards

---
Parthan.S.R.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070326/c50584bb/attachment.html>


More information about the Python-list mailing list