Python Forum
Convert string to float problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert string to float problem
#9
This gets a str object:
arduinoData = ser.readline().decode('ascii')
This tries to divide the str by 5
data = (arduinoData)/5
Dividing a str by an int raises a TypeError which is caught by the overly broad except: Your program prints "fail" every time, no matter what is read
You should do this:
data = float(arduinoData)/5
This might also fail, but if arduinoData is a string that can be interpreted as a float, it will succeed.
ndc85430 likes this post
Reply


Messages In This Thread
Convert string to float problem - by vasik006 - May-27-2022, 07:15 PM
RE: Convert string to float problem - by ndc85430 - May-27-2022, 07:33 PM
RE: Convert string to float problem - by deanhystad - May-27-2022, 08:12 PM
RE: Convert string to float problem - by vasik006 - May-28-2022, 08:13 AM
RE: Convert string to float problem - by vasik006 - May-28-2022, 08:16 AM
RE: Convert string to float problem - by vasik006 - May-28-2022, 08:46 AM
RE: Convert string to float problem - by deanhystad - May-28-2022, 01:08 PM
RE: Convert string to float problem - by vasik006 - Jun-03-2022, 04:54 PM
RE: Convert string to float problem - by deanhystad - Jun-03-2022, 06:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert to bin problem kucingkembar 3 328 Apr-19-2024, 12:53 PM
Last Post: kucingkembar
  python calculate float plus float is incorrect? sirocawa 6 404 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Convert From PDf into JPEG Problem koklimabc 4 1,118 Sep-05-2023, 06:44 AM
Last Post: Gribouillis
  convert string to float in list jacklee26 6 2,004 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 6,227 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,425 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,659 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,953 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Detecting float or int in a string Clunk_Head 15 4,740 May-26-2022, 11:39 PM
Last Post: Pedroski55
  Convert a string to a function mikepy 8 2,615 May-13-2022, 07:28 PM
Last Post: mikepy

Forum Jump:

User Panel Messages

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