Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
929 views
in Technique[技术] by (71.8m points)

debugging - "ValueError: zero length field name in format" error in Python 3.0,3.1,3.2

I'm trying learn Python (3 to be more specific) and I'm getting this error:

ValueError: zero length field name in format

I googled it and I found out you need to specify the numbers:

a, b = 0, 1
if a < b:
     print('a ({0}) is less than b ({1})'.format(a, b))
else:
     print('a ({0}) is not less than b ({1})'.format(a, b))

And not like the tutorial (from lynda.com) actually says to do:

a, b = 0, 1
if a < b:
     print('a ({}) is less than b ({})'.format(a, b))
else:
     print('a ({}) is not less than b ({})'.format(a, b))

The tutorial im following has Python 3.1, and im using 3.2 and what i read about this error is that this only happens in <3.1 (3.0). Did they undo this in 3.2, or am i doing something wrong?

Also, speak slowly ;) this is literally my first night learning Python and only the 2nd "script" i've written in Python.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Python 2.6 and 3.0 require the field numbers. In Python 2.7 and later and 3.1 and later, they can be omitted.

Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.

python2.6.4>>> print '|{0:^12}|{1:^12}|'.format(3,4)
|     3      |     4     |

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...