Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87426

Re: Odd ValueError using float

From Peter Otten <__peter__@web.de>
Subject Re: Odd ValueError using float
Date 2015-03-14 17:08 +0100
Organization None
References <mailman.336.1426284641.21433.python-list@python.org> <877fuk71fz.fsf@nightsong.com> <CAPTjJmoxL0ux3mHAd8UCZmdbTHe_Yz5i9Y8XrzJ7NAuy8WZ4sA@mail.gmail.com> <me1k2i$ljt$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.358.1426349303.21433.python-list@python.org> (permalink)

Show all headers | View raw


emile wrote:

> On 03/13/2015 08:09 PM, Chris Angelico wrote:
>> On Sat, Mar 14, 2015 at 1:33 PM, Paul Rubin <no.email@nospam.invalid>
>> wrote:
>>> emile <emile@fenx.com> writes:
>>>> *** NameError: name 'val' is not defined
>>>> (Pdb) l
>>>> 139         try:
>>>> 140             val = round(float(decval),1)
>>>> 141         except:
>>>> 142             import pdb; pdb.set_trace()
>>>
>>> If 'float' or 'round' throw an exception, the assignment to 'val' never
>>> happens, so 'val' is undefined.  Try examining the value of 'decval' in
>>> the debugger to see what is making the conversion fail.
>>
>> That's exactly what the OP did :) Trouble is, it didn't help, because
>> it sure looked as if decval was the string '4'. My best guess was -
>> and is - that it's not just a string. We're looking at an SQL
>> interface routine here, so it may be that there's a string subclass
>> that length-limits itself, on the assumption that it's going into a
>> fixed-length database field.
> 
> It ran almost to completion before generating the error again --
> 
> (Pdb) decval
> '4'
> (Pdb) type(decval)
> <type 'str'>
> (Pdb) len(decval)
> 1
> (Pdb) int(decval)
> *** ValueError: invalid literal for int() with base 10:
> '41.700000000000003'
> 
> So there's still something amiss.

Why are you checking

int(decval)

when the actual failing expression is

round(float(decval),1)

? Try to evaluate the latter in the debugger, and if that gives no clue 
change

>>>> 139         try:
>>>> 140             val = round(float(decval),1)
>>>> 141         except:
>>>> 142             import pdb; pdb.set_trace()

to just

val = round(float(decval), 1)

to get a meaningful traceback and post that.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Odd ValueError using float emile <emile@fenx.com> - 2015-03-13 15:10 -0700
  Re: Odd ValueError using float Paul Rubin <no.email@nospam.invalid> - 2015-03-13 19:33 -0700
    Re: Odd ValueError using float Chris Angelico <rosuav@gmail.com> - 2015-03-14 14:09 +1100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 08:28 -0700
    Re: Odd ValueError using float Chris Angelico <rosuav@gmail.com> - 2015-03-15 02:52 +1100
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-14 17:08 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 10:08 -0700
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 10:17 -0700
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-14 19:24 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-14 13:01 -0700
    Re: Odd ValueError using float Peter Otten <__peter__@web.de> - 2015-03-15 15:01 +0100
    Re: Odd ValueError using float emile <emile@fenx.com> - 2015-03-17 13:48 -0700

csiph-web