I try to calculate my taxes using python, but I get:
TypeError: can't multiply sequence by non-int of type 'float'
Code:
price_without_taxes = driver.find_element_by_xpath("//div[@id='__next']/div/main/div/div/div/p/span").text
print('LE PRIX EST DE :' price_without_taxes)
tps = price_without_taxes * 0.05
tvq = price_without_taxes * 0.09975
price_with_taxes = float((price_without_taxes tps tvq))
print("Sous-Total: " "%1.2f" % price_with_taxes "$\n")
print("Livraison: " str(10) "$\n")
print("Total: " "%1.2f" % price_with_taxes "$\n")
CodePudding user response:
Your variable, prices_without_taxes
is a string rather than an int
/float
. You can not multiply text by float
s. You would have to convert your string to an int/float frist: float(prices_without_taxes) * (multiple)
CodePudding user response:
price_without_taxes
is a string, not a number.
In order to multiply it by float
you have to convert it to a number.
python is "clever" enough to multiple string containing a number only by int
, however it doesn't know how to multiply a string containing a number by float
.
This should work better:
price_without_taxes = driver.find_element_by_xpath("//div[@id='__next']/div/main/div/div/div/p/span").text
print('LE PRIX EST DE :' price_without_taxes)
#converting "price_without_taxes" from string to float object
price_without_taxes = float(price_without_taxes)
tps = price_without_taxes * 0.05
tvq = price_without_taxes * 0.09975
price_with_taxes = float((price_without_taxes tps tvq))
print("Sous-Total: " "%1.2f" % price_with_taxes "$\n")
print("Livraison: " str(10) "$\n")
print("Total: " "%1.2f" % price_with_taxes "$\n")