Be careful with these other answers recommending simply to cast to int, since any floating point value can be truncated to an integer successfully.
You may need to check that the floating point representation of the value is equal to its integer representation, so that, e.g., 3 and 3.0 will count as an integer but not 3.5.
>>> def is_it_a_number(value): ... try: ... float(value) ... print('It can be represented as floating point!') ... except ValueError: ... print('It cannot be represented as floating point.') ... else: ... try: ... if float(value) == int(float(value)): ... print('It is a valid integer.') ... except ValueError: ... print('It is not a valid integer.') ... >>> is_it_a_number(3) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(-3) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(3.0) It can be represented as floating point! It is a valid integer. >>> is_it_a_number(3.5) It can be represented as floating point! >>> is_it_a_number('3.0') It can be represented as floating point! It is a valid integer. >>> is_it_a_number('3.5') It can be represented as floating point! >>> is_it_a_number('sandwich') It cannot be represented as floating point.
intand catch the exception? EX:int(input("input an integer"))with throw aValueErrorfor any non-integer value. The same would work for afloat. Unless you're explicitly not allowed to use Python's built in facilities for doing this.3.0to represent an integer?