[Tutor] Not-so Quick Question

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 26 Apr 2001 07:12:10 -0700 (PDT)


On Thu, 26 Apr 2001, Glen Wheeler wrote:

>   Hey all,
> 
>   I've got this tkinter program which is going to be an arkanoid clone (eventually).  However, it (seemingly) randomly dumps with this error :
> 
> Traceback (most recent call last):
>   File "C:\WINDOWS\Profiles\Glen\Desktop\etc\pystuff\the bouncy ball game.py", line 280, in gogamego
>     co = Screen.coords(block)
>   File "c:\python20\lib\lib-tk\Tkinter.py", line 1929, in coords
>     self.tk.splitlist(
> ValueError: invalid literal for float(): 27.0expected


Hmmm... Do you have any string that looks like "27.0expected"?  The
problem with float() is that if you're going to convert something into a
float, the whole string needs to have a floatlike quality to it.  For
example:

###
>>> float(27.0)
27.0
>>> float("27.0")
27.0
>>> float("27.0a")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): 27.0a     
###

So if there's even a hint of extra stuff in the string, it won't
successfully bend itself into a float.  That's what they mean by invalid
literal.


A similar thing happens with the int() function:

###
>>> int("27.0")
Traceback (innermost last):
  File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 27.0   
>>> int(float("27.0"))
27
###

and this was a bit of a shock to me, that it actually takes two passes to
get the string "27.0" into an integer form.

Hope this helps!