Hello,
I have an SQLite Statement that fetches one result.
Output:
(1.5,)
The problem is when I try using that result for math:
MaterialUsedPrice = SelectedItemQuantity * SellPrice
print('Price: $',MaterialUsedPrice)
I get this:
Error:
MaterialUsedPrice = SelectedItemQuantity * SellPrice
TypeError: can't multiply sequence by non-int of type 'tuple'
Is there a way to convert the tuple result to a float or something so I can use it in my Multiplication statement?
Thanks in advance?
Code Snippet:
#Connect to the inventory database (inventory.db)
connection = sqlite3.connect(InventoryDatabase)
cursor = connection.cursor()
cursor.execute("SELECT Sell_Price_$ FROM items WHERE Name = ?",(SelectedItemName,))
connection.commit()
SellPrice = cursor.fetchone()
# #Update the quantity
# cursor.execute("UPDATE items SET Quantity = Quantity - ? WHERE Name = ?",(SelectedItemQuantity, SelectedItemName,))
# connection.commit()
#Close the connection
connection.close()
MaterialUsedPrice = SelectedItemQuantity * SellPrice
print('Price: $',MaterialUsedPrice)
What is a tuple? If you really think about what a tuple is, the answer to your question should be obvious.
One way, would be to unpack the tuple...
fpn = tuple[0]
... where
fpn
is a variable to hold the floating point number and
tuple
is the tuple to be unpacked.
(Aug-01-2022, 08:31 PM)rob101 Wrote: [ -> ]One way, would be to unpack the tuple...
fpn = tuple[0]
... where fpn
is a variable to hold the floating point number and tuple
is the tuple to be unpacked.
I tried that already.
x = SellPrice[0]
print(x)
#Calculate the Price of Material Used
MaterialUsedPrice = SelectedItemQuantity * x
print(SelectedItemName, ' Used Price: $',MaterialUsedPrice)
This is what I got (Which is right and that's what I want, but it won't let me do anything with it):
Output:
1.5
Followed By:
Error:
MaterialUsedPrice = SelectedItemQuantity * x
TypeError: can't multiply sequence by non-int of type 'float'
Are you sure that TypeError
is not coming from SelectedItemQuantity
?
(Aug-01-2022, 08:47 PM)rob101 Wrote: [ -> ]Are you sure that TypeError
is not coming from SelectedItemQuantity
?
Ya I'm sure.
Even if I do this:
x = SellPrice[0]
#Calculate the Price of Material Used
MaterialUsedPrice = 2 * x
print(SelectedItemName, ' Used Price: $',MaterialUsedPrice)
I get:
Error:
x = SellPrice[0]
TypeError: 'NoneType' object is not subscriptable
Humm... okay, but that's not what I see from your last post...
(Aug-01-2022, 08:40 PM)Extra Wrote: [ -> ]x = SellPrice[0]
print(x)
(Aug-01-2022, 08:40 PM)Extra Wrote: [ -> ]Output:1.5
So, have you tried a debug print?
print(type(SellPrice))
x = SellPrice[0]
print(type(x))
print(type(SelectedItemQuantity))
You have a lot of errors.
This error indicates that SelectedItemQuantity is a sequence, not a number.
Error:
x = SellPrice[0]
MaterialUsedPrice = SelectedItemQuantity * x
TypeError: can't multiply sequence by non-int of type 'float'
I get the same error when I run this:
SelectedItemQuantity = [1, 2, 3]
SellPrice = (1.23,)
print(SelectedItemQuantity * SellPrice [0])
The error is caused by SelectedItemQuantity , not SellPrice.
This error is probably caused by a failed query.
Error:
x = SellPrice[0]
TypeError: 'NoneType' object is not subscriptable
I get the same error if I do this:
SellPrice = None
x = SellPrice[0]
Do you test if fetchone() returns None? According to the documentation:
Quote:fetchone()
Fetch the next row of a query result set as a tuple. Return None if no more data is available.
None is an expected return value. You might get None because there was no matches to your query, or you might get None because you already fetched all the results. Your program needs to check for these conditions.
(Aug-01-2022, 09:07 PM)rob101 Wrote: [ -> ]Humm... okay, but that's not what I see from your last post...
(Aug-01-2022, 08:40 PM)Extra Wrote: [ -> ]x = SellPrice[0]
print(x)
(Aug-01-2022, 08:40 PM)Extra Wrote: [ -> ]Output:1.5
So, have you tried a debug print?
print(type(SellPrice))
x = SellPrice[0]
print(type(x))
print(type(SelectedItemQuantity))
I get:
Output:
<class 'tuple'>
<class 'float'>
<class 'str'>
So my SelectedItemQuantity is of type String which cannot be multiplied to a float (x = SellPrice[0]) but even if I try to do
2*x
for testing
I get:
x = SellPrice[0] TypeError: 'NoneType' object is not subscriptable
Which confuses me because an int and a float should be able to be multiplied together.
Full Code Snippet:
def SubmitClicked(self):
#Get User Inputted Quantity & Material
for widget in self.MaterialUsedFrame.children():
if isinstance(widget, QSpinBox):
SelectedItemQuantity = widget.text()
if isinstance(widget,QComboBox):
SelectedItemName = widget.currentText()
print(SelectedItemQuantity, ',' ,SelectedItemName)
#Connect to the inventory database (inventory.db)
connection = sqlite3.connect(InventoryDatabase)
cursor = connection.cursor()
cursor.execute("SELECT Main_Category FROM items WHERE Name = ?",(SelectedItemName,))
connection.commit()
Result = cursor.fetchone()
#Close the connection
connection.close()
print(Result)
if Result == ('Wire',):
#Connect to the inventory database (inventory.db)
connection = sqlite3.connect(InventoryDatabase)
cursor = connection.cursor()
cursor.execute("SELECT Price_Per_Ft FROM items WHERE Name = ?",(SelectedItemName,))
connection.commit()
PricePerFt = cursor.fetchone()
#Close the connection
connection.close()
print(PricePerFt)
#Might be better off making all those SpinBoxes Labels
#& Just setting the text to the price
for widget in self.MaterialPriceFrame.children():
if isinstance(widget, QDoubleSpinBox):
widget.setValue(80.80)
else:
#Connect to the inventory database (inventory.db)
connection = sqlite3.connect(InventoryDatabase)
cursor = connection.cursor()
cursor.execute("SELECT Sell_Price_$ FROM items WHERE Name = ?",(SelectedItemName,))
connection.commit()
SellPrice = cursor.fetchone()
# #Update the quantity
# cursor.execute("UPDATE items SET Quantity = Quantity - ? WHERE Name = ?",(SelectedItemQuantity, SelectedItemName,))
# connection.commit()
#Close the connection
connection.close()
print('Selcted Item Quantity: ', SelectedItemQuantity)
print(type(SellPrice))
x = SellPrice[0]
print(type(x))
print(type(SelectedItemQuantity))
#Calculate the Price of Material Used
MaterialUsedPrice = SelectedItemQuantity * SellPrice
print('Total ', SelectedItemName, ' Used Price: $', MaterialUsedPrice)
#----------------------------------------------------------------------------------------------------
So SelectedItemQuantity is a str. You can't do this either:
x = "1"
y = (1.23,)
print(x * y[0])
Error:
print(x * y[0])
TypeError: can't multiply sequence by
non-int of type 'float'
You are lucky that your SellPrice is a float. If it was an int Python would happily make bizarre results without any error message ('1' * 2 == '11')