On Wed, Jun 7, 2017 at 3:09 AM, Stephen Tucker <stephen_tuc...@sil.org> wrote:
> I have just been thrown through an unecessary loop because of an unhelpful
> error message.
>
> I am running Python 2.7.10 on a Windows 10 machine.
>
>>>> print float ("")
>
> Traceback (most recent call last):
>   File "<pyshell#60>", line 1, in <module>
>     print float ("")
> ValueError: could not convert string to float:
>>>>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> The unhelpful message is the response that the system gives when you try to
> convert a null string to a floating point number. Now, in the example I
> have given here, it is obvious that the string is null, but in the example
> that threw me into a loop, it was not obvious.
>
> It would be more helpful is the system replied:
>
> ValueError: could not convert null string to float
>
> Any chance of that becoming the case?
>

Actually it's already telling you, but the empty string isn't easy to
read there. Compare:

>>> float("asdf")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: asdf
>>> float("asd")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: asd
>>> float("as")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: as
>>> float("a")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: a
>>> float("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float:

You could ask for the empty string to be special-cased for clarity,
but the information is definitely there. Or perhaps showing the repr
of the string would be clearer.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to