Python Forum
conditions not working as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conditions not working as expected
#11
Yes. Does that approach work with what you want to do with the test function?
Reply
#12
(Mar-26-2023, 01:09 PM)deanhystad Wrote: Yes. Does that approach work with what you want to do with the test function?

In theory yes it should work. I will try it because I have multiple entities in a that row that I need to use to calculate the final number. I will try and get back to the post.
Reply
#13
(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 taxbill 
I get the following error
Error:
TypeError: unsupported operand type(s) for |: 'str' and 'str'
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 taxbill 
it does not generate the error. i can not figure out why. Both are strings
Reply
#14
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:
Output:
True False
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
Reply


Forum Jump:

User Panel Messages

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