Python Forum

Full Version: try function working on PC but not raspberry pi
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,
I have a function which checks if an entry is a number or not. The code works fine on my pc which is running python 3.7.9 (bottom right had corner of Thonny). However, on my raspberry pi this code doesn't work. My raspberry pi is running python 3.7.3. For some reason sudo apt-get upgrade python3 didn't upgrade my pi to the same version as my PC.
Regardless, on my Pi when i try float("not a numebr") it returns an error, so i would assume that try function would work even despite the lower version number of python. Any thoughts?
def isNumber(argument):
    try:
        float(argument)
        prnit("Is number")
        return 1
    except:
        print("not a number")
        return 0
    
num = "777"
float(num)
print(isNumber(num))
I would expect float("not a numebr") to return an error for any version of python (a ValueError exception).

One problem you have is that you are catching all errors with the except: on line 6. You should instead catch only the ValueError thrown by the float that you might expect.

This causes a problem because line 4 has a typo (prnit instead of print). That syntax error it caught by line 6 and assumed to be a problem with the float. You don't print any information about the error.

Fix your typo and change the except line to except ValueError:

There's no difference here with your python version. I assume that you don't have an exact copy and only one of them has the typo in it.