On 26 Mar 2007 05:00:54 -0700, sandeep patil <[EMAIL PROTECTED]> 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.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to