1

Why doesn't the int() function convert a float to integer while in input() function?

input_1 = input(f'enter the num: ')

try: 
    a = int(input_1)
    print(f"it is an integer")
except:
    print(f"no an integer")
    

input_1 = 3.532453

try: 
    a = int(input_1)
    print(f"it is an integer")
except:
    print(f"no an integer")

Result:

enter the num: 3.532453
no an integer
it is an integer
3
  • 1
    Can you pls explain what you mean? Commented Dec 24, 2021 at 13:59
  • So first thing, you're trying to make an app that checks if a number is an integer or a float? Also what version of python are you using. Commented Dec 24, 2021 at 14:01
  • 1
    @RaedAli I'm using 3.9.0 version of python Commented Dec 24, 2021 at 14:03

5 Answers 5

2

Bear in mind that the input_1 value when you request for an input is not a float, is a string

input_1 = input(f'enter the num: ')
print(type(input_1)) # ​<class 'str'>

So with the cast you are trying to convert a string with value 3.532453 into a integer.

If you cast this string to a float first and then you cast it into a integer, it will work.

input_1 = input(f'enter the num: ')
try: 
    a = int(float(input_1))
    print(f"it is an integer") 
except: 
    print(f"no an integer")
Sign up to request clarification or add additional context in comments.

Comments

2

The input function returns a string (type str in python). The int function accepts a string as long as this string represents a valid integer (base 10 by default). This means when you pass a string that contains a ., int will fail because the string is not a valid integer.

>>> input_1 = input(f'enter the num: ')
enter the num: 3.532453
>>> print(type(input_1))
<class 'str'>
>>> int(input_1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.532453'

On the other hand, when you do input_1 = 3.532453, you are creating a variable input_1 of type float. The int function accepts float arguments and casts them to integer by stripping the floating point part.

>>> input_1 = 3.532453
>>> print(type(input_1))
<class 'float'>
>>> int(input_1)
3

Comments

1

print(int(123.456)): you can convert a non-integer to int

print(int("123456")): you can convert a string representing an integer to int

print(int("123.456")): you cannot convert a string representing a non-integer to int

I mean, you can, but not by using the int(...) operation directly.

Comments

-1

Interestingly you can convert an integer antered as a string to a float with no problems as follows:

input_1 = float(input (f'enter the num: '))
print(input_1)

But when you try a similar line of code to convert a float to an integer you get an error as follows:

input_1 = int(input (f'enter the num: '))
print(input_1)

enter the num: 44.5

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in <cell line: 1>() ----> 1 input_1 = int(input (f'enter the num: ')) 2 print(input_1)

For your question, you need to convert the string input into a float first, then convert it into an integer as follows:

input_1 = int(float(input (f'enter the num: ')))
print(input_1)

enter the num: 55.23

55

Comments

-4

So you can change a decimal to an int in python. But not without losing all the data past the decimal point. For example if you convert "12.3356" to an int using int(float("12.3356") and the result would be 12.

An int value in python is one without a decimal point for eg: 3.

A float value in python is a number with a decimal point for eg: 3.141.

So, if you want to check if what a user enters is a number and conserve everything past the decimal point, convert the strings to a float instead of an int:

input_1 = input('enter the num: ')

try:
    a = float(input_1)
    print("it is a number")
except ValueError:
    print("not a number")

By using float() instead of int(), you get to keep all the data past the decimal point (if you need it in other parts of your program).

However, if you still want to convert a float to an int (and lose everything past the decimal point), you can do:

Note that the below is basically extra code for a disadvantage and the above is usually better (in most cases), but if that's what you want well here you go:

input_1 = input('enter the num: ')

try:
    a = int(float(input_1))
    print("it is a number")
except ValueError:
    print("not a number")

2 Comments

you can't change a decimal to an int in python - that's wrong. Check my answer here.
you can't change a decimal to an int in python - as bbbbbbbbb said: this is wrong, you can cast a decimal to an int (losing the decimal part, of course). The point is that input_1 is not a decimal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.