Unhelpful error message

Peter Otten __peter__ at web.de
Tue Jun 6 13:52:00 EDT 2017


Stephen Tucker wrote:

> Hi,
> 
> 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.
> 
> I incorporate sample output from IDLE:
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
>>>> print float ("123.456")
> 123.456
>>>> 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?

"null string" sounds a lot like C. The most obvious fix in Python would be 
to apply repr() which Python 3 already does for some strings...

>>> float("-")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '-'
>>> float("1x")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '1x'

...but not the empty string:

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

Maybe there were some backward compatibility concerns that I lack the 
fantasy to imagine.




More information about the Python-list mailing list