Python Forum
Convert SQLite Fetchone() Result to float for Math
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert SQLite Fetchone() Result to float for Math
#11
So I can do this:
                        print('Selcted Item Quantity: ', SelectedItemQuantity)

                        x = float(SelectedItemQuantity)
                        y = SellPrice[0]
                        z = float(y)
                        print('Float Value = ', x)
                        print('x*z= ', x*z)
                        MaterialUsedPrice2 = x*z

                        #Calculate the Price of Material Used
                        # MaterialUsedPrice = SelectedItemQuantity * SellPrice
                        print('The Total Price for the ', '(',x,')', SelectedItemName, ' Used is: $', MaterialUsedPrice2)
And I get:
Output:
Selcted Item Quantity: 2 Float Value = 2.0 x*z= 3.0 The Total Price for the ( 2.0 ) 1/2" PVC Coupling Used is: $ 3.0
Which is what I want,
But then I get this error and the program crashes:
Error:
y = SellPrice[0] TypeError: 'NoneType' object is not subscriptable
Any way to get around/fix this?
Reply
#12
As previously mentioned, fetchone() can return None. You need to check the return value before trying to use it.
Axel_Erfurt and ndc85430 like this post
Reply
#13
As @deanhystad says; if there's a possibility that the sell price is Null, then you need to deal with that by catching it, to stop your script from crashing out.

You could use a try / except or a if branch. I'm guessing that the sell price should never be Null or even zero, so you may want to look in to why that condition occurs.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#14
I think it is a bad idea to verify actual values returned from a database query. Maybe 0 can be, or worse is a valid sell price. The future maintainer of your code will be confused that database queries for free items always fail and they will curse you when they find the reason is buried deep in some method.

Verify values before you put them in the database. Verify that a query found a match. Assume values returned by the query are correct.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 311 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  convert result to hex ascii Jusufs 6 1,564 May-19-2023, 03:21 PM
Last Post: deanhystad
  math formula does not give the same result as bash script [SOLVED] AlphaInc 3 981 Apr-02-2023, 07:21 PM
Last Post: AlphaInc
  convert string to float in list jacklee26 6 1,930 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  openpyxl convert data to float jacklee26 13 6,033 Nov-19-2022, 11:59 AM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,425 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  math.log versus math.log10 stevendaprano 10 2,426 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 3,782 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  SQL and fetchone Scorpio 6 6,558 Oct-14-2020, 02:24 PM
Last Post: beebert69
  ValueError: could not convert string to float: RandomCoder 3 5,776 Jul-27-2020, 07:38 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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