Home > Blockchain >  Solution to '>' not supported between instances of 'NoneType' and 'int&#
Solution to '>' not supported between instances of 'NoneType' and 'int&#

Time:10-18

I've referenced other questions on this error but can't seem to find a solution for my case.

a = function(x)   
b = function(y)

if a == None:
   print("Invalid a")
if b == None
   print("Invalid b")
if a > b:
   print("Review")

Error: '>' not supported between instances of 'NoneType' and 'int'

I understand what the error means based on my research, but I'm not certain on how to fix it. Any help would be apprecaited.

CodePudding user response:

You may use elif:

a = function(x)   
b = function(y)

if a == None:
   print("Invalid a")
elif b == None
   print("Invalid b")
elif a > b:
   print("Review")

Otherwise, there's still the possibility that a or b is None as all comparisons are independent of each other in your code.

CodePudding user response:

As an alternative to an if/else statement, I'd suggest using a try/except block, which should be slightly more efficient if the except condition is generally not satsified.

Also, per PEP 8 the comparison with None should be with is or is not, instead of == and !=.

a = 21
b = None

try:
    if a > b:
        print("Review")

except TypeError:
    if a is None:
       print("Invalid a")
    elif b is None:
       print("Invalid b")
    else:
        raise
  • Related