Python Forum
try function working on PC but not raspberry pi
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try function working on PC but not raspberry pi
#1
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))
Reply
#2
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.
Underscore likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked kindly support with this dropna function not working gheevarpaulosejobs 2 670 Jul-24-2023, 03:41 PM
Last Post: deanhystad
Exclamation Function Not Working Alivegamer 7 1,877 Jul-19-2022, 01:03 PM
Last Post: deanhystad
  Input() function not working in VS Code darpInd 7 13,325 Feb-17-2020, 03:28 PM
Last Post: snippsat
  Unrelated new function causing existing working function to fail Nick_G 2 2,264 Jan-27-2020, 07:21 PM
Last Post: Nick_G
  User Defined function not working Raj_Kumar 4 3,018 Dec-17-2019, 12:44 PM
Last Post: buran
  Function not working eglaud 0 1,648 Oct-24-2019, 03:30 PM
Last Post: eglaud
  Invoking function in if else statement, not working! ibaad1406 13 5,619 May-30-2019, 09:05 PM
Last Post: ibaad1406
  function not working tryingtolearnpython 2 2,965 Jul-23-2018, 02:54 AM
Last Post: tryingtolearnpython
  function for SQLite query not working pythonNoob 1 2,798 May-16-2018, 05:21 PM
Last Post: Larz60+
  import keyboard module doesn't get found - working on laptop but no on raspberry pi.. HANSJORG2 1 8,167 Mar-16-2018, 02:48 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020