Mar-26-2023, 01:09 PM
Yes. Does that approach work with what you want to do with the test function?
conditions not working as expected
|
Mar-26-2023, 01:09 PM
Yes. Does that approach work with what you want to do with the test function?
Mar-26-2023, 01:31 PM
Mar-28-2023, 05:35 PM
(Mar-26-2023, 01:09 PM)deanhystad Wrote: Yes. Does that approach work with what you want to do with the test function? yes this does work and found did some learning in the meantime as well regarding what I needed to be done. I do have one follow up questions. I have this code here: def taxcalculation(owneroccupied, homesteadcode, exemptclass, countyrealestate, staterealestate, countycredit, statecredit): from rates import annearundelsolidwaste, annearundelstormwater if (exemptclass != "Blank"): if (owneroccupied == "Yes" | owneroccupied == "No"): taxbill = annearundelsolidwaste + annearundelstormwater return taxbill if (exemptclass == "Blank"): if (owneroccupied == "Yes"): if (homesteadcode == "Approved" | homesteadcode == "Denied"): taxbill = countyrealestate + staterealestate - countycredit - statecredit + annearundelsolidwaste + annearundelstormwater return taxbill else: taxbill = countyrealestate + staterealestate + annearundelsolidwaste + annearundelstormwater return taxbill else: taxbill = 0 return taxbillI get the following error If I switch it to this: [python]def taxcalculation(owneroccupied, homesteadcode, exemptclass, countyrealestate, staterealestate, countycredit, statecredit): from rates import annearundelsolidwaste, annearundelstormwater if (exemptclass != "Blank"): if (owneroccupied == "Yes" | owneroccupied == "No"): taxbill = annearundelsolidwaste + annearundelstormwater return taxbill if (exemptclass == "Blank"): if (owneroccupied == "Yes"): if (homesteadcode == "Approved" or homesteadcode == "Denied"): taxbill = countyrealestate + staterealestate - countycredit - statecredit + annearundelsolidwaste + annearundelstormwater return taxbill else: taxbill = countyrealestate + staterealestate + annearundelsolidwaste + annearundelstormwater return taxbill else: taxbill = 0 return taxbillit does not generate the error. i can not figure out why. Both are strings
Mar-28-2023, 10:51 PM
(This post was last modified: Mar-28-2023, 10:51 PM by deanhystad.)
What do you think "|" is supposed to do in this context?
(owneroccupied == "Yes" | owneroccupied == "No")Do you think it does this? (owneroccupied == "Yes" or owneroccupied == "No")Python does this: (owneroccupied == ("Yes" | owneroccupied) == "No")And since "Yes" and owneroccupied are both str, it looks to see if str | str is supported. It is not. In Python. "|" is a bitwise or, not a logical or. As in C, uses for "|" are very specific, and it is not commonly seen in Python code. A better way to check if a str matches one of many str's is to use "in" owneroccupied in ("Yes", "No")Be thankful that Python raised an exception. If you got away with using "|" in such a statement, I'm pretty sure the results would be a surprise. You cannot do bitwise or with strings, but you can do it with int. What do you think the following code will print? print(5 == 4 | 1 == 5) print(5 == 4 | 1 == 1)From the explanation above you know that Python sees this as: print(5 == (4 | 1) == 5) print(5 == (4 | 1) == 1)1 == 0b001, 4 == 0b100 and 5 == 0b101 1 | 4 == 0b101 == 5 Replacing the bitwise or with the result: print(5 == 5 == 5) print(5 == 5 == 1)And the output from the program: So if you ever have 3 variables and you want to know if they all have the same value, you can write this:a == b == c # instead of a == b and b == c |
|