Error inside Try: Invalid literal for in() with base 10: ''

Hi All,

I’m getting the error below, and I have no idea why.

It’s a simple function intended to convert integers stored as strings into proper integers.
It’s been working well, but for no apparent reason, it’s now not working. I’ve tried resetting the script editor and restarting Rhino, but it hasn’t helped.

Any suggestions?

Does it work correctly if you use a name other than string for your variable (like strg)? I think string might be reserved by Python as a module name.

I was using “str” previously, but i figured that was probably reserved… I’ll try something else and report back…

Dunno, I just tested this here in the ScriptEditor:

#! python 3

def StrToInt(string):
    try:
        return int(string)
    except:
        return string

print(StrToInt("123"))
print(StrToInt("ABC"))

and it seems to work correctly - even in IP2, so I don’t know what might be wrong…

That is because a string that contains other characters than can be converted from, say a string starting with “Freigh” will throw the ValueError like you are seeing. The message you get is for that ValueError. With the value given for the input variable you should see the function return on line 83.