0

I could use some guidance as I'm new to python and trying to figure out if we can use integer values of 0 and 1 in order to return our own customized message within the same function where I'm trying to reverse the Boolean values.

def reverse_bool_value(value):
    if value == True:
        return not value
    elif value == False:
        return not value
    elif type(value) == int:
        return "Not Boolean Value"
    else:
        return "Boolean Expected"
    


>>>reverse_bool_value(12)
'Not Boolean Value'

>>>reverse_bool_value(1)
False

>>> reverse_bool_value(0)
True

The function works fine if I enter in any integers (except 0 and 1), float, complex, string values and Boolean conditions of True and False, it returns me the expected outcome.

Accept when I use 0 or 1...it does not return me the customized message given "Not Boolean Value", rather it returns True for 0 (as the condition is reversed) and False for 1. I appreciate that using both inside the same condition of validating between integer and bool does not work, as quite evident. But I would really appreciate some help, as to how should I go around it in order for this to work. Is there a way?? Thank you.

K_menon
  • 11
  • 4
  • 1
    Note that `type(int)` is the class `type`. You meant `type(value) == int`. – Thierry Lathuille Sep 12 '21 at 07:21
  • Hi @Henry Ecker Thank you for clarifying, but I was unable to get specific answers for my question. When using boolean and integer in same function...the function is unable to differentiate. Do you mind just taking a look at it? The link you posted does not clarify of having a different outcome. – K_menon Sep 12 '21 at 07:28
  • I really appreciate if you would be able to look into it @ Henry Ecker – K_menon Sep 12 '21 at 07:29
  • I just edited your code block. @ThierryLathuille closed your question. – Henry Ecker Sep 12 '21 at 07:29
  • @ThierryLathuille. Thank you for clarifying, but I was unable to get specific answers for my question. When using boolean and integer in same function...the function is unable to differentiate. Do you mind just taking a look at it? The link you posted does not clarify of having a different outcome. – K_menon Sep 12 '21 at 07:30
  • 1
    @ Henry Ecker my apologies for misunderstanding that – K_menon Sep 12 '21 at 07:30
  • @ThierryLathuille I really would appreciate if would take a look into it please. Thank you – K_menon Sep 12 '21 at 07:31
  • What do you find unclear in the duplicate? It clearly states that `True` is equal to `1`, so if you set `value` to 1, `value == True` will be True. If you want to test if you have a bool or an int, just do it before comparing the value to 0 or 1... – Thierry Lathuille Sep 12 '21 at 07:44
  • Cheers @ThierryLathuille. I was able to find the solution. However I also took a different route. Pasting my answer here, so if there are students who wants to refer can do so. – K_menon Sep 12 '21 at 07:54
  • def identify_1_0(num): if num == 0 or num == 1: return "Boolll" def reverse_bool_value(value): if type(value) == int: return identify_1_0(value) elif value == False: return True elif value == True: return False else: print("Boolean Expected ") return type(value) reverse_bool_value(1) – K_menon Sep 12 '21 at 07:54
  • 1
    `if isinstance(value, bool): return not value else: return 'Not a bool'`… – deceze Sep 12 '21 at 08:10
  • @deceze this could solve the time complexity as well. – K_menon Sep 12 '21 at 08:13

0 Answers0