Home > Back-end >  Trying to check whether function argument is an integer in python
Trying to check whether function argument is an integer in python

Time:11-13

I am trying to write a function in python that runs if the parameter is an integer or returns an error message. This is what I have.

def an_int(n_int):
    if type(n_int) is int:
        print("good")
    else:
        print("bad")

Can someone help?

CodePudding user response:

I didn't really understand what you are asking but I hope this clarifies your question:

def is_int(n_int):
  if type(n_int) is int:
    print(str(n_int) ' is an integer')
  else:
    print(str(n_int) ' is not an integer')

value = 'gfjk'
is_int(value)
# Prints 'gfjk is not an integer'

value = 5
is_int(value)
# Prints '5 is an integer'

CodePudding user response:

def an_int(n_int):
    if isinstance(n_int, type):
        print("good")
    else:
        print("bad")
  • Related