-3
def get(count=None): 
    if count >= 1: 
        a = count - 1
    else: 
        a = 0
    return a

Everything is in the title.. Just for sport.

Thank you

John Doe
  • 1,570
  • 3
  • 13
  • 22

1 Answers1

2

You mean using a ternary operator?

a = count - 1 if count >= 1 else 0

Your code will fail if count is None because you can't compare nonetypes to integers. But my answer is how you would write this conditional statement in a "better" way.


Thus - I would write the function like this (Thanks @poke for the max idea.):

def get(count=None):
    return max(count-1, 0) if isinstance(count, int) else 0
Community
  • 1
  • 1
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131