# Assert Method and Booleans

**URL:** <https://discuss.python.org/t/assert-method-and-booleans/84683>\
**Category:** Python Help\
**Created:** [March 15, 2025, 10:16pm UTC](https://discuss.python.org/t/assert-method-and-booleans/84683 "2025-03-15T22:16:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CJCodingJourney](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@CJCodingJourney](https://discuss.python.org/u/CJCodingJourney)\
**Post date:** [March 15, 2025, 10:16pm UTC](https://discuss.python.org/t/assert-method-and-booleans/84683/1 "2025-03-15T22:16:38Z")

</div>

I’m a noob a coding on VS Studio Code. So basically, I’m wondering why I can’t add the ‘or’ boolean in this code on, like line 17. I get the expected expression error by pylance. Why?

def fair(employee, ceo, ratio):  
“”"Decide whether the ratio of CEO to employee pay is fair.

```
Parameters:
    employee: average employee pay
    ceo: CEO pay
    ratio: the fair ratio
    
Return: 
   a Boolean indicating whether ceo / employee is fair
   
Precondition: employee is a float or integer that is > 0 ceo is a float or integer that is >= 0 ratio is a float or integer that is >= 0 and <= 0

Postcondition: returns a boolean statement- True or False dependent whether or not (ceo / employee) <= ratio
"""

assert isinstance(employee, float) , 'n should be a float or a integer' or assert isinstance(employee, int) , 'n should be a float or an integer'

assert employee > 0, 'employee is not an indentured servant'

assert(ceo, float or int), 'ceo is getting paid $ dumbass'

assert(ratio, float or int), 'What ratio is not a fraction/decimal or integer?'

return (ceo / employee) <= ratio

```

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [March 16, 2025, 2:19am UTC](https://discuss.python.org/t/assert-method-and-booleans/84683/2 "2025-03-16T02:19:29Z")

</div>

Hello,

from your post description and referencing your script, I think that you are referring to these two lines:

> [@CJCodingJourney](#):
>
> why I can’t add the ‘or’ boolean in this code on, like line 17

```python
assert isinstance(employee, float) # , 'n should be a float or a integer' or assert 
isinstance(employee, int) # , 'n should be a float or an integer'

```

You can do as you have done for both `ceo` and `ratio` variables in your script. Alternatively, instead of using the `or` for checking two assertions, you may use the comma (`,`), as in:

```python
# These two lines are equivalent:
assert isinstance(employee, int or float)
assert isinstance(employee, float), isinstance(employee, int) # equivalent to using `or` in theory

```

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [March 16, 2025, 2:53am UTC](https://discuss.python.org/t/assert-method-and-booleans/84683/3 "2025-03-16T02:53:40Z")

</div>

`int or float` evaluates to `int` because `int` is _truthy_, so:

```python
assert isinstance(employee, int or float)

```

is the same as:

```python
assert isinstance(employee, int)

```

The `assert` statement evaluates the first expression and raises `AssertionError` if it’s false, using the value of the second expression, if present, so in:

```python
assert isinstance(employee, float), isinstance(employee, int)

```

if `isinstance(employee, float)` is false, it raises `AssertionError` with the result of `isinstance(employee, int)`, which is either `False` or `True`.

Neither of the above is equivalent to an `or`.

The correct form is either:

```python
assert isinstance(employee, float) or isinstance(employee, int), 'n should be a float or an integer'

```

or:

```python
assert isinstance(employee, (float, int)), 'n should be a float or an integer'

```

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [March 16, 2025, 3:18pm UTC](https://discuss.python.org/t/assert-method-and-booleans/84683/4 "2025-03-16T15:18:30Z")

</div>

Since you are purposefully asserting an `exception` based on the outcome of an `isinstance` test, you can couple it with a `try` / `except` pair to catch the exception. It would help if you tested each case individually to know the source of the `exception` as opposed to multiple cases tested together where you wouldn’t.

In the following test script, each test case is tested along with a `try` / `except` pair. If a test case fails, the `exception` is caught and a corresponding print statement is generated to alert the source of the `exception`.

```python
from colorama import Fore

employee = 4.561 # Test variable

try:
    assert isinstance(employee, float) # test if type 'float'
except AssertionError:
    print(Fore.RED + f"'employee' value {employee} is not a type float.")

try:
    assert isinstance(employee, int) # test if type 'int'
except AssertionError:
        print(Fore.RED + f"'employee' value {employee} is not a type int.")

```
