0

This is the code

def reverse(arg):
    if arg == False or arg == True:
        return not arg
    else:
        return("boolean expected")





for some reason when running the function with 0 as a parameter it returns True instead of "boolean expected"

  • 2
    Does this answer your question? [is-false-0-and-true-1-an-implementation-detail-or-is-it-guaranteed-by-the](https://stackoverflow.com/questions/2764017), [why-does-1-true-but-2-true-in-python](https://stackoverflow.com/questions/7134984) – stovfl Jul 10 '20 at 15:27

1 Answers1

1

This is an ideal place to use Python's isinstance function:

replace line 2 of this function with:

if isinstance(arg, bool):

and you should get the desired effect.

This is necessary because Python will readily evaluate integers to bools, so the isinstance function makes the check more strict.

MHealton
  • 31
  • 2