"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.

62481 次浏览

我猜你是不小心跑了 Python 2.6。

This feature is only available for at least 3.1 if you are using python 3, or 2.7 if you are using python 2.

Python 2.6 and 3.0 require the field numbers. 在 Python 2.7及更高版本中 and 3.1 and later, they can be omitted.

在版本2.7中更改: 位置参数说明符可以是 省略,因此“{}{}”等效于“{0}{1}”。

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

If you're using Eclipse you should look into Window -> Preferences -> PyDev -> Interpreter - Python. There you have a list of interpreters (with name and location). If for your current project you're using interpreter which is located for example in /usr/bin/python then probably executing /usr/bin/python -V whill give you something like "Python 2.6.6". And there is your answer like Winston Ewert wrote.

(只需单击“ New...”按钮并将/usr/bin/python3作为“ location”添加新的 Interperter。然后,您可能需要更改项目设置(参数-> PyDev-Interpreter/Grammar)。