سؤال

I have this code:

ab = {  'Swaroop'   :   'swaroop@swaroopch.com',
            'Larry'        :    'larry@wall.org',
            'Matsumoto' :   'matz@ruby-lang.org',
            'Spammer'   :   'spammer@hotmail.com'
        }

print "Swaroop's address is", ab['Swaroop']

del ab['Spammer']

print '\nThere are {} contacts in the address-book\n'.format(len(ab))

for name, address in ab.items():
    print 'Contact {} at {}'.format(name, address)

ab['Guido'] = 'guido@python.org'

if 'Guido' in ab:
    print "\nGuido's address is", ab['Guido']

and i am getting this error in the GUI:

Traceback (most recent call last):
  File "E:/Python26/cook.py", line 11, in <module>
    print '\nThere are {} contacts in the address-book\n'.format(len(ab))
ValueError: zero length field name in format

Why is this happening since the len(ab) = 3?

How can I have zero length field name in format ?

هل كانت مفيدة؟

المحلول

In Python 2.6, you need to specify the index, for which the data corresponds to, explicitly, like this

print '\nThere are {0} contacts in the address-book\n'.format(len(ab))
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top