3/15 is_int Best Practices


#1

The code I came up with was:

def is_int(x):
  if x > 0 and x - int(x) == 0:
    return True
  if x < 0 and x - int(x) == 0:
    return True
  if x == 0:
  	return True
  else:
    return False

It works, but is there a way to do it with fewer lines of code?
How can you do it with the fewest lines of code?


#2

surely, but the challenge is to do this own your own, optimize the code. Its good practice

however, fewest lines of code doesn’t always means its best. It might obscure readability

how would you go about making this code shorter? What are you thinking? If you explain your thought process, i might be able to help you shorten the program, while you learn a lot from it


#3

I just did

def is_int(x):
  if x == round(x):
    return True
  else:
    return False

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.