我有这段代码:
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']
我在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
为什么这是因为len(ab)= 3?
我如何拥有zero length field name in format
?
答案 0 :(得分:3)
在Python 2.6中,您需要明确指定数据所对应的索引,如此
print '\nThere are {0} contacts in the address-book\n'.format(len(ab))