0

I guys, im a noob on the programming world and im putting my best to learn Python.

Im stuck with this problem right now:

"Make an algorithm that verifies if a certain value is an integer."

Now the problem is that the INPUT function returns the type in string. If I cast the variable to FLOAT it stops me from reading the integer numbers.

What should i do?

val1 = float(input("enter value")
if type (val1)== int:
print("value is integer")
else:
print("value is NOT integer")
  • Welcome to Stack Overflow. Have a look at this question, which explains how to convert a string into an integer https://stackoverflow.com/questions/2262333/is-there-a-built-in-or-more-pythonic-way-to-try-to-parse-a-string-to-an-integer – Yennefer Dec 31 '18 at 15:30
  • Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – Yennefer Dec 31 '18 at 15:31

5 Answers5

3

You can try to covert the string input into an integer or float:

k = input("enter value:")
try:
    k = int(k)
    # it is an integer
except ValueError:
    try:
        k = float(k)
    except ValueError:
        print("Neither int nor float")
    else:
        print(k, "is a float")
else:
    print(k, "is an integer")

Output:

enter value:44.4
44.4 is a float

enter value:hallo 
Neither int nor float

enter value:44
44 is an integer

Doku:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
2

You can use try and except for this (you can give it a look at Errors and Exceptions. Try turning the input into an int, and write the corresponding print statements depending on the result:

val1 = input("enter value")
try:
    int(val1)
    print("value is integer")
except:
    print("value is NOT integer")
yatu
  • 86,083
  • 12
  • 84
  • 139
1

If you want also input like '3.0' to be recognized as integers (so all whole numbers, instead of all values of type int), just calling int on it is not enough, since int cannot parse that string.

Another way to determine if a value is a whole number is to see if there is a rest if divided by one, so you can use the modulo operator %:

val1 = float(input("enter value"))
if val1 % 1 == 0:
    print("value is integer")
else:
    print("value is NOT integer")
Graipher
  • 6,891
  • 27
  • 47
0

If you don’t want casting and error handling. You can use ast.literal_eval that transform string to Python objects and isinstance, which comes with Python, to solve your issue.

from ast import literal_eval


in_val = input('Enter value: ')

# get literal value

l_val = literal_eval(in_val)

# check if it is int
if isinstance(l_val,int):
    print('It is integer')
elif isinstance(l_val,float):
    print('It is a float')
elif isinstance(l_val,str):
    print('It is a string')

else:
    print('Not int,float or string')# could be list, dict etc ;)
Prayson W. Daniel
  • 14,191
  • 4
  • 51
  • 57
-1
if val1.count('.') == 0:
   print("value is NOT integer")
else:
   print("value is integer")
GTDiablo
  • 13
  • 4